Link to home
Start Free TrialLog in
Avatar of ramrocket
ramrocket

asked on

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;
}
Avatar of mahesh1402
mahesh1402
Flag of India image

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
Avatar of ramrocket
ramrocket

ASKER

can you explain how this cause a memory leak?  how to use LPCTSTR pointer in this case?

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

MAHESH
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
>> 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
ASKER CERTIFIED SOLUTION
Avatar of mahesh1402
mahesh1402
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this works!