SendMessage CString

I am sending this from a thread:
...
AfxGetMainWnd()->SendMessage(WM_MY_MESSAGE, 0 , new CString(L"Delete Me") );

I am wondering if this creates a memory leak even doing a delete.

LRESULT ::OnMyMessage(WPARAM wp, LPARAM lp)
{
    CString *pString = (CString *) lp;
    delete pString, pString=0;
}
ramrocketAsked:
Who is Participating?
 
mahesh1402Commented:
e.g

SendMessage(WM_MY_MESSAGE, 0 ,(LPARAM) (LPCTSTR)"Delete Me")

LRESULT ::OnMyMessage(WPARAM wp, LPARAM lp)
{
    LPCTSTR pString = (LPCTSTR)lp;
    ...

}
0
 
mahesh1402Commented:
new CString(L"Delete Me") <==== causes memory leak of the CString object.
It's better to use LPCTSTR pointer OR just at destruction delete the CString object..no need to ue new operator.


MAHESH
0
 
ramrocketAuthor Commented:
can you explain how this cause a memory leak?  how to use LPCTSTR pointer in this case?

thanks,
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
mahesh1402Commented:
what happens if you just send
AfxGetMainWnd()->SendMessage(WM_MY_MESSAGE, 0 ,CString(L"Delete Me") ); <==

MAHESH
0
 
ramrocketAuthor Commented:
the reason I use new because a pointer to CString can be casted to LPARAM.  the above usage of CString on the stack will give an error:  cannot convert from CString to LPARAM
0
 
mahesh1402Commented:
>> how to use LPCTSTR pointer in this case?
ie.

SendMessage(WM_MY_MESSAGE, 0 ,(LPARAM) (LPCTSTR)"Delete Me") <== now handle this in your function as LPCTSTR

MAHESH
0
 
ramrocketAuthor Commented:
this works!
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.