Send object using SendMessage in MFC (delete)

Posted on 2011-04-26
Medium Priority
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
  • 3
LVL 32

Accepted Solution

phoffric earned 2000 total points
ID: 35470673
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 32

Expert Comment

ID: 35470689
>> 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.

Author Closing Comment

ID: 35470715
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 32

Expert Comment

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

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

864 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