Re: Derived destructor not being called
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@raytheon.com> wrote in message
news:1146441731.471931.96960@e56g2000cwe.googlegroups.com...
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!