Re: Derived destructor not being called

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



In addition to other comments, it's WRONG to use reinterpret_cast to cast
between class pointers. It won't do any pointer adjustment.

<stanley_r_eisenberg@xxxxxxxxxxxx> wrote in message
news:1146441731.471931.96960@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi there,

I've been working on an IDL implementention and I'm having problems
with one of the objects' destructors not being called.

The follow is the structure of my IDL file:

interface IMyBase : IUnknown {
....
}

interface IMyDerived : IMyBase {
....
}

Now for an implementation, there is no implementation of IMyBase since
I meant it to be used as a base. But we do have...

Class CMyDerived : public IMyDerived {
public:
CMyDerived();
~CMyDerived()
}

Now to move onto the problem, elsewhere we have a vector containing
pointers:

vector<IMyDerived*> myVector

And now onto the problem, the following will work with the exception
that it doesn not call the destructor!

// stuff things in the vector
for(int i=0; i<10; i++) {
IMyDerived* pitem = NULL;
hr = CoCreateInstance(CLSID_CStuff,
NULL,
CLSCTX_ALL,
IID_IMyDerived,
(void**)&pitem);

IMyBase* pitem2 = reinterpret_cast<IMyBase*>(pitem);
myVector.push_back(pitem2);
}

// now we want to delete the stuff.....
// I'm hoping that the destructor in CMyDerived will be called

// attempt 1, this doesn't work
IMyBase* d = myVector[0];
delete d; // no luck
delete [] d; // no luck

// attempt 2, still no luck
IMyBase* d = myVector[0];
IMyDerived* d2 = reinterpret_cast<IMyDerived*>(d);
delete d2; // no luck
delete [] d2; // no luck

// since this stuff is in a COM DLL, I even tried using some of the
IUNKNOWN mechanisms
(myVector[0])->Release(); // no luck
((IMyDerived*) myVector[0])->Release(); // no luck


Nothing I do seems to get my derived class' destructor to go off which
it causing a memory leak over time.

Does anyone know how I can fix this problem?

I was thinking about adding a virtual destructor to the base, but since
the base is only defined via an IDL interface I have no idea how to do
this... or if this would even work... or even on the right track...

Thanks!



.



Relevant Pages

  • Re: Derived destructor not being called
    ... I've been working on an IDL implementention and I'm having problems ... interface IMyDerived: IMyBase { ... // I'm hoping that the destructor in CMyDerived will be called ... keep un-addref'd pointers to COM objects in containers, ...
    (microsoft.public.vc.language)
  • Derived destructor not being called
    ... I've been working on an IDL implementention and I'm having problems ... interface IMyDerived: IMyBase { ... // I'm hoping that the destructor in CMyDerived will be called ...
    (microsoft.public.vc.language)
  • Re: Missing destructor calls on exit
    ... Some from a modeless dialog (whose ... Try steping through the destructor to find what goes wrong. ... up with wild pointers to where the object was. ... The other dll might or might not have a seperate heap. ...
    (microsoft.public.vc.mfc)
  • Re: couple of general questions
    ... >might seem kinda beginner. ... >for two years and as I know,I only put a setructor when creating a pointers ... >cases of simple classes where we dont use pointers neither destructor ... While not questioning your practical experience and expertise I think ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Allowing at most 2 instances
    ... >i had a couple of cout statements before and after the try. ... >i figured it might have to do with memory cleanup...but i am not really sure ... Remember that, normally, there's one destructor call to match each ... the client manage the pointers (rather than trying to keep them ...
    (alt.comp.lang.learn.c-cpp)