Send object using SendMessage in MFC (delete)

Posted on 2011-04-26
Last Modified: 2012-05-11
Hi Experts,

I'd like to send a string or two and an integer using SendMessage or PostMessage.  Let's say I have an object:
pair<string, int> * p = new pair<string, int>();
p->first = "this";
p->second = 5;
PostMessage(WM_MY_MESSAGE, 0, (LPARAM) p );

I don't want memory leaks - how do I know if p was deleted on the other side?  i.e., I have to be sure someone handled the message and deleted p.  Simple enough, but I guess there's no way to delete it from the message sender since PostMessage will only be handled long after the calling function has already returned right?

Just thinking out loud and playing devil's advocate....  Let me know your thoughts please!

Question by:thready
    LVL 31

    Accepted Solution

    You are referring to client/server programs on different machines (or at least different address spaces on the same machine)?

    In that case, you are responsible for memory leaks in the program that you write. If you do a new in your program, then you are expected to do the corresponding delete.

    You could just define myPair as not a pointer, fill in first and second

    pair<string, int> myPair;
    myPair.first = "this";
    myPair.second = 5;
    PostMessage(WM_MY_MESSAGE, 0, (LPARAM) &myPair );

    Open in new window

    and then not have to worry about deleting, since myPair will be removed as soon as it goes out of scope (i.e., when returning from the {} block in which it was defined (e.g., returning from a function).
    LVL 31

    Expert Comment

    >> I don't want memory leaks - how do I know if p was deleted on the other side?
    If you are responsible for writing the other side, then you are responsible for new and corresponding delete. If you are not responsible for the other side, then when they complain about "your" program causing their program to crash, you have to go calmly into finger-pointing mode, and either help them or prove via TCP dumps that you are not causing "their" problem.
    LVL 1

    Author Closing Comment

    of course - I don't need to call new in the first place.  I was worried that the data would go out of scope, but it's copied when sent by value ( as long as the objects are of known simple types).  Thanks!
    LVL 31

    Expert Comment

    Glad to be of assistance. It always helps to have an extra set of eyes.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
    Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now