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