Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6218303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:34:57+00:00 2026-05-24T07:34:57+00:00

I’m following the Microsoft sample code given for How to create a sink interface

  • 0

I’m following the Microsoft sample code given for “How to create a sink interface in a MFC-based COM client” 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.

The thing that is giving me fits is the very last note of the sample:

Because CMySink was created on the heap, make sure you delete it to
avoid memory leaks.

A couple of things I’m noting:

  • 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.
  • 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.

Keeping in mind that final note in the sample text, my clean-up code looks something like this:

//Get a pointer to sinks IUnknown, no AddRef.
LPUNKNOWN pUnkSink = m_pSink->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;

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.

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.

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:34:58+00:00Added an answer on May 24, 2026 at 7:34 am

    The default implementation of OnFinalRelease is to delete the object. It would probably be best to subclass CCmdTarget to gain the message map and event hooks and make your own class which can stay in scope for all processing needed then only delete once at the end. You could add a callback or notification message to signal being done with everything to clean your object up.

    You don’t want to be allocating and deleting from the heap in a loop for performance reasons if nothing else anyway.

    Update:

    If the default implementation is undesirable, as is the case often with MFC, then your best option is to override the virtual OnFinalRelease and have it do nothing instead of calling delete this; like it does in CCmdTarget. Then it’s entirely on you to delete your object and clean up after yourself. I’d make your sink class reference counted object or put it in a smart pointer of some kind like std::unique_ptr if you have C++0x support.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have just tried to save a simple *.rtf file with some websites and
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.