Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Destructor ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 16 Topic 9528 of 9823
Post > Topic >>

Re: Destructor not called with forward declaration instead of include

by Triple-DES <DenPlettfrie@[EMAIL PROTECTED] > Apr 22, 2008 at 10:52 AM

On 22 Apr, 04:43, internetpet <internet...@[EMAIL PROTECTED]
> wrote:
> Say you have these files:
>
> BigClazz.h
>
>      class Clazz; // Note the forward declaration only , no include
> "Clazz.h"
>      class BigClazz
>      {
>          public:
>              BigClazz();
>              ~BigClazz();
>              Clazz* pclazz;
>      };
>
> BigClazz.cpp
>
>      #include "BigClazz.h"
>      BigClazz::BigClazz(){}
>      BigClazz::~BigClazz()
>      {
>          delete pclazz; // Here the destructor of of the Clazz object
> will not be called
>      }
>
> Clazz.h
>
>      class Clazz
>      {
>          public:
>              Clazz();
>              ~Clazz();
>      };
>
> Clazz.cpp
>
>      #include "Clazz.h"
>      Clazz::Clazz(){};
>      Clazz::~Clazz(){};
>
> main.cpp
>
>      int main(int argc, char *argv[])
>      {
>          BigClazz* pVM = new BigClazz();
>          pVM->pclazz = new Clazz();
>          delete pVM;
>      }
>
> If you run this you'll see that the Clazz destructor will not be
> called when BigClazz does "delete pclazz;" in it's own destructor. But
> if you replace the forward declaration in BigClazz (class Clazz;) with
> an include
> (#include "Clazz.h") then it works.
>
> Any idea why?

Yes. According to [expr.delete]/5 in the standard (with my comments in
parens):

If the object being deleted has incomplete class type (in this case,
forward declared) at the point of deletion and the complete class has
a non-trivial destructor (you declared your own dtor, so it's non-
trivial) or a deallocation function, the behavior is undefined (which
usually means not calling the destructor in this case).

To fix this, simply #include "Clazz.h" in BigClazz.cpp, so that the
type is complete at the point of deletion.

-- 
DP

      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 16 Posts in Topic:
Destructor not called with forward declaration instead of includ
internetpet <internetp  2008-04-21 20:43:18 
Re: Destructor not called with forward declaration instead of in
=?ISO-8859-1?Q?Daniel_Kr=  2008-04-22 10:54:58 
Re: Destructor not called with forward declaration instead of in
Triple-DES <DenPlettfr  2008-04-22 10:52:59 
Re: Destructor not called with forward declaration instead of in
Travis Vitek <travis.v  2008-04-22 10:53:33 
Re: Destructor not called with forward declaration instead of in
marlow.andrew@[EMAIL PROT  2008-04-22 11:07:11 
Re: Destructor not called with forward declaration instead of in
Maciej Sobczak <see.my  2008-04-22 11:13:17 
Re: Destructor not called with forward declaration instead of in
alasham.said@[EMAIL PROTE  2008-04-22 11:18:19 
Re: Destructor not called with forward declaration instead of in
Brendan <catphive@[EMA  2008-04-22 11:13:28 
Re: Destructor not called with forward declaration instead of in
nickf3 <nickf3@[EMAIL   2008-04-22 11:43:17 
Re: Destructor not called with forward declaration instead of in
Alan Johnson <awjcs@[E  2008-04-22 11:43:18 
Re: Destructor not called with forward declaration instead of in
peter koch larsen <pet  2008-04-22 15:22:47 
Re: Destructor not called with forward declaration instead of in
Vidar Hasfjord <vattil  2008-04-23 13:13:18 
Re: Destructor not called with forward declaration instead of in
Triple-DES <DenPlettfr  2008-04-23 13:13:15 
Re: Destructor not called with forward declaration instead of in
Seungbeom Kim <musiphi  2008-04-26 08:42:07 
Re: Destructor not called with forward declaration instead of in
Carl Barron <cbarron41  2008-04-27 07:01:39 
Re: Destructor not called with forward declaration instead of in
Vidar Hasfjord <vattil  2008-04-27 07:44:19 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 1:36:24 CDT 2008.