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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mahesh1402IT ProfessionalCommented:
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
ramrocketAuthor Commented:
can you explain how this cause a memory leak?  how to use LPCTSTR pointer in this case?

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

MAHESH
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

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
mahesh1402IT ProfessionalCommented:
>> 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
mahesh1402IT ProfessionalCommented:
e.g

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

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

}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ramrocketAuthor Commented:
this works!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.