Where does the destructor code for things I have defined in an ATL COM object belong?
Should it go in ~MyComClass()
or in MyComClass::FinalRelease()
?
Where does the destructor code for things I have defined in an ATL COM object belong?
Should it go in ~MyComClass()
or in MyComClass::FinalRelease()
?
As long as FinalRelease
is in question, I assume your question is related to ATL.
In most cases you can clean things up in either of the two. FinalRelease
will be called immediately before the actual destructor. The important difference is that if you aggregate other objects, FinalRelease gives you a chance to clean the references and release dependencies before actual destructor of top level COM object class (esp. CComObject
) starts working.
That is, you clean things up in two steps, first references to aggregated objects in FinalRelease
and then other things in either FinalRelease
or destructor.
This is the general approach:
MyComClass::~MyComClass()
{
// Cleanup object resources in here.
}
ULONG __stdcall MyComClass::Release()
{
ref_count_--;
if (0 == ref_count_)
{
delete this;
return 0;
}
return ref_count_;
}
EDIT: FinalRelease()
appears to be related to ATL which I am unfamiliar with.
new CComObject<T>
, which inherits from T
and adds implementations for AddRef
and Release
in the most derived class -- this way, the delete this;
works correctly with a non-virtual destructor. –
Whitecollar © 2022 - 2024 — McMap. All rights reserved.
::Release()
method to my class, yet presumably this reference counting is going on somewhere? How can I find it? – Ascariasis