destructor is called before copy-constructor on temporary class (allocated on stack during function call) 
Author Message
 destructor is called before copy-constructor on temporary class (allocated on stack during function call)

code:

class CUnmanagedA

{...

  do(CUnamanagedB cu)

...}

not working code:

__gc class CManagedA

{...

    do() {  cUnmanagedA1->do(cUnamanagedB1); }

...}

working code:

__gc class CManagedA

{...

    do() {  cUnamanagedB cub1; cUnmanagedA1->do(cub1); }

...}

Within a function call in a managed library I am calling unmanaged code
(s.a.):

The instance cUnamanaged2 is passed by value. When I debug this code I see
the following actions:

-copy constructor of cUnamanagedB1 -> cUnamanagedB2

-desctructor ~cUnamanagedB of instance cUnamanagedB2

function CUnmanagedA->do

desctructor of  ~cUnamanagedB for an unknown this pointer.

desctructor of  ~cUnamanagedB for cUnamanagedB2

This means, the destructor is called one time more the constructor, which is
not expected.

If I create an instance before calling the function (see working code)
everything is fine.

For me it looks like if a copy of cUnamanagedB2 was created just by memory
copy but without calling the copy constructor.

In my case CUnamanagedB is a reference counter. Therefore creation and
destruction of objects is important.

I tried to reproduce this error by a simple example, but the behaviour is
different (no copy constructor call at all) and everything is working.

Any ideas or suggestions?

Did I do something wrong?

Thanks

Toni



Mon, 04 Oct 2004 22:27:19 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. compiler bug: destructor for temporary in constructor initializer never called

2. constructors,destructors, & copy construcotrs

3. Copy Constructor : Copying Base Class Member Variables

4. Destructor in class not called when class goes out of scope

5. LNK2001 for template class constructor and destructor!

6. Linker error with template class: No constructor/destructor

7. Protected constructor/destructor by Class Wizard

8. Unneccessary copy-constructor calls?

9. operator= calling copy constructor

10. copy constructor is called instead of assignment operator!

11. Adding an object to a vector so that copy-constructor is called

12. Copy Constructors/assignment operators on derived classes

 

 
Powered by phpBB® Forum Software