Re: Derived destructor not being called
- From: "Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@xxxxxxxxxxxxxxx>
- Date: Mon, 1 May 2006 07:08:19 -0700
stanley_r_eisenberg@xxxxxxxxxxxx wrote:
See the problem that I'm getting is that I can't destroy the items
stored in the vector. Each item has a huge amount of data. I'm not
interested in destroying the COM object per se, but rather that
instance of the COM interface.
You can only destroy objects - interfaces are ethereal things that do not
occupy memory.
So far all my attempts to get the destructor invoked on any of the
items in the vector have failed.
OK, let's back up a bit. You want CHolder::deleteFromVector to delete the
CDerived object at the specified index, is that correct?
To do that, you should be calling AddRef() in the IBase* pointer in
CHolder::addToVector(), and calling Release() on that same IBase* pointer in
CHolder::deleteFromVector. Keep in mind that an object created by
CoCreateInstance has a reference count of 1 by the time you get a pointer to
it. Keep in mind also that if you use a COM smart pointer in the vector,
all of this will happen automatically (i.e. use
std::vector<_com_ptr_t<IBase> > instead of std::vector<IBase*> as your
collection).
I've been able to destroy my reference to the class, but not the class
itself so the memory usage stays constant.
How is CDerived::Release implemented?
I need a way to destroy those items because HugeData eats up alot of
memory.
While Release() will decrement the count, it does not destroy the
object because, in this case, two objects were created and only one
was deleted.
Each object needs to store it's own reference count. In your example, three
objects were created: d1, d2, h1. Assuming that the CHolder methods behave
as indicated above, you'd have the following reference counts:
IDerive *d1;
CoCreateInstance(... d1 ...);
// d1 reference count == 1
d1->setHugeData(getLotsOfData());
IDerive *d2;
CoCreateInstance(... d2 ...);
// d2 reference count == 1
d2->setHugeData(getLotsOfData());
IHolder *h1;
CoCreateInstance(... h1 ...);
// h1 reference count == 1
h1->addToVector(d1);
// d1 reference count == 2
h1->addToVector(d2);
// d2 reference count == 2
h1->deleteFromVector(1);
.... you'll note that after executing the code above that your objects won't
have been deleted since the caller is still holding references to them. You
need to call Release() on all of the pointers that you got from
CoCreateInstance. In this case, calling d1->Release() right after
CHolder::addToVector(d1) would probably be the right thing (assuming this
line of code doesn't access d1 by name anymore after that).
That's ok, but I need to have a way to ensure that the per instance
state of each implementation is destroyed.
I don't know what you mean by this. Do you want an "empty" CDerived after
this, or no CDerived at all? If the former, please explain in more detail
exactly what it is you're trying to do.
Hopefully this scenario better explains my problem.
We're getting there. Keep asking questions!
-cd
.
- Follow-Ups:
- Re: Derived destructor not being called
- From: stanley_r_eisenberg
- Re: Derived destructor not being called
- From: stanley_r_eisenberg
- Re: Derived destructor not being called
- From: stanley_r_eisenberg
- Re: Derived destructor not being called
- References:
- Derived destructor not being called
- From: stanley_r_eisenberg
- Re: Derived destructor not being called
- From: Carl Daniel [VC++ MVP]
- Re: Derived destructor not being called
- From: stanley_r_eisenberg
- Re: Derived destructor not being called
- From: stanley_r_eisenberg
- Re: Derived destructor not being called
- From: Carl Daniel [VC++ MVP]
- Re: Derived destructor not being called
- From: stanley_r_eisenberg
- Derived destructor not being called
- Prev by Date: Re: is file exist
- Next by Date: Re: Derived destructor not being called
- Previous by thread: Re: Derived destructor not being called
- Next by thread: Re: Derived destructor not being called
- Index(es):
Relevant Pages
|