COM ATL destructor
Hi to all,
I have a simple question about COM and ATL: why is my COM-object destructor NOT called? Constructor is called, but destructor not! AddRef() and Release() are done properly, DLL is unloaded properly, but destrutor isn't called! WHY? Please can anyone help me?
[283 byte] By [
mwolfgang] at [2007-11-18 19:28:25]

# 1 Re: COM ATL destructor
Think this is a bit of a COM-ness related to debugging. Generally, you should use FinalConstruct() and FinalRelease():
override these in your headers
STDMETHOD(FinalConstruct)();
STDMETHOD_(void, FinalRelease)();
and use this in your cpp's
STDMETHODIMP CMyClass::FinalConstruct()
{
// ... blah ...
}
STDMETHODIMP_(void) CMyClass::FinalRelease()
{
// ... blah ...
}
I suspect this will solve your issue, or at least help with the debugging.
Toot
Toot at 2007-11-11 1:17:10 >

# 2 Re: COM ATL destructor
If you want to be sure that it stops at the breakpoint, you can do a "hard breakpoint" by putting __asm int 3 inside the destructor.
However, it's better to use FinalConstruct and FinalRelease anyways for everything that's not a POD (plain old datatype, i.e. int, char etc.) and especially for any other COM objects.
Yves M at 2007-11-11 1:18:11 >

# 3 Re: COM ATL destructor
Thx a lot for your reply, but ...
FinalConstruct() is called BUT FinalRelease() is NOT called. I don't know WHY!!??!!
# 4 Re: COM ATL destructor
Are you using proper COM methods - i.e. CCI and object's Release method? How are you so sure it isn't being called? Have you tried using standard C calls to write something to a file (rather than set breakpoint or message box) in FinalRelease()? Have you done anything non-standard COM to your object? Do you share instances around? How?
That's enough questions for now :) ...
Toot
Toot at 2007-11-11 1:20:09 >

# 5 Re: COM ATL destructor
If FinalRelease is not called, then the object's reference count doesn't go down to 0. You probably don't do things right in your test application.
Another issue can be circular references. I.e. object A holds a reference to B (with AddRef / Release on construction / deletion) and B holds a reference to A. In this case, you either have to release one object earlier than in the destruction or not AddRef / Release it in one of the objects.
Yves M at 2007-11-11 1:21:10 >

# 6 Re: COM ATL destructor
Hmmm that's right in that it might do what you want but I have to say be very, very careful with circular references and be exceedingly wary of using and/or passing references around that haven't been AddRef'd first. Generally, if you find yourself needing to implement circular references, something in your design needs addressing. Maybe the circular part can be eliminated by introducing a third object?
Toot at 2007-11-11 1:22:10 >
