Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the proper lifetime management for a CCmdTarget used as an event sink?
    text
    copied!<p>I'm following the Microsoft sample code given for <a href="http://support.microsoft.com/kb/181845" rel="nofollow">"How to create a sink interface in a MFC-based COM client"</a> in order to create an event sink in C++ (VC6). The event source is a .NET assembly that exposes it's functionality via COM interop.</p> <p>The thing that is giving me fits is the very last note of the sample:</p> <blockquote> <p>Because CMySink was created on the heap, make sure you delete it to avoid memory leaks.</p> </blockquote> <p>A couple of things I'm noting:</p> <ul> <li>The auto-increment / decrement parameter of GetIDispatch and AfxConnectAdvise / AfxConnectUnadvise is set to FALSE, so I'm assuming the internal reference count for the sink remains unchanged throughout the exercise.</li> <li>The OnFinalRelease method is not shown in the exercise, so I'm assuming it is the default behavior of deleting the instance of the sink.</li> </ul> <p>Keeping in mind that final note in the sample text, my clean-up code looks something like this:</p> <pre><code>//Get a pointer to sinks IUnknown, no AddRef. LPUNKNOWN pUnkSink = m_pSink-&gt;GetIDispatch(FALSE); //Terminate a connection between source and sink. //m_pUnkSrc is IUnknown of server obtained by CoCreateInstance(). //m_dwCookie is a value obtained through AfxConnectionAdvise(). AfxConnectionUnadvise(m_pUnkSrc, IID_MYEVENT, pUnkSink, FALSE, m_dwCookie); delete m_pUnkSink; </code></pre> <p>The code this sample is part of runs in a loop that involves creating the sink, wiring it up, waiting for a few events, then tearing it down and deleting it. What I am seeing is that, after a few rounds through the loop, OnFinalRelease is called out of the blue. Not only that, OnFinalRelease is being called on the instance of the sink for the current iteration of the loop (not some previous instance used by a prior iteration of the loop). The effect is that the current sink gets deleted out from under the current loop execution, and a bunch of null pointer errors result.</p> <p>I tried removing the call to delete m_pUnkSink. The result is OnFinalRelease is never called. This leaves me with a memory leak, as all of those instances of the sink accumulate in the heap.</p> <p>I think I could probably get away with reusing the same instance of the sink for each iteration of the loop, but I'm curious what the correct lifetime management is. Do I need to make an explicit choice between deleting the instance myself and overriding OnFinalRelease to do nothing vs. never deleting myself and always expecting OnFinalRelease to perform the delete? Is one preferred to the other?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload