Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1737
  • Last Modified:

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;
}
0
ramrocket
Asked:
ramrocket
  • 4
  • 3
1 Solution
 
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
 
mahesh1402Commented:
what happens if you just send
AfxGetMainWnd()->SendMessage(WM_MY_MESSAGE, 0 ,CString(L"Delete Me") ); <==

MAHESH
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

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

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

}
0
 
ramrocketAuthor Commented:
this works!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now