Send object using SendMessage in MFC (delete)

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!

Thanks,
Mike
LVL 1
threadyAsked:
Who is Participating?
 
phoffricCommented:
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).
0
 
phoffricCommented:
>> 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.
0
 
threadyAuthor Commented:
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!
0
 
phoffricCommented:
Glad to be of assistance. It always helps to have an extra set of eyes.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.