Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CString causing memory leaks when being reassigned

Status
Not open for further replies.

nyjil

Programmer
Jul 14, 2000
32
US
Jeez, I hope someone can help me.<br><br>I'm using edit boxes to display CStrings via the UpdateData(FALSE).&nbsp;&nbsp;At times the variable attached to the edit box are changed.&nbsp;&nbsp;Whenever I change the CString variable the gosh-darn thing causes a memory leak.&nbsp;&nbsp;Why? I have no idea and that is my question.&nbsp;&nbsp;The edit box in question is part of a child window that is started by a DoModal() in the parent window.&nbsp;&nbsp;I was under the impression that I could change the CString variable at anytime by simply reassigning it and then give the command UpdateData(FALSE) and the computer would display the newly reassigned value in the edit box.&nbsp;&nbsp;Example<br><br><br>//Assigns value to variable CString 'text' and displays on screen in an edit box.<br>text = &quot;Hello&quot;;<br>UpdateData(FALSE);<br><br>//causes a memory leak when 'text' is reassigned.<br>text = &quot;Goodbye&quot;;<br>UpdateData(FALSE);<br><br>I get the intended result and the program does redisplay the edit box with the newly reassigned value, but now for some reason a memory leak occurs.&nbsp;&nbsp;Why is this? Please I've been pulling out my hair for over a month trying to figure out this one.&nbsp;&nbsp;Do I need to clear the value of &quot;Hello&quot; before assigning &quot;Goodbye&quot;?&nbsp;&nbsp;If so, how do I do this?&nbsp;&nbsp;Does the fact that the edit box is in a child window instead of the main window have anything to do with my problem?&nbsp;&nbsp;Any help would be appriciated<br><br>Thank you<br>Nyjil
 
Dear Nyjil,<br><br>What you describe should not cause a memory leak. Have you installed all the SP's for your OS and your Visual C++?<br><br>What version of VC are you running?<br>What OS are you on?<br>How are you determining there is a memory leak?<br><br>-pete
 
Palbano,<br><br>I am running VC 6.0 and am on Windows 98.&nbsp;&nbsp;I am determining that there is a memory leak by using:<br><br>(begining of function)<br>#ifdef _DEBUG<br> CMemoryState oldMemState, newMemState, diffMemState;<br> oldMemState.Checkpoint();<br>#endif<br><br>//function itself....<br><br>#ifdef _DEBUG<br> newMemState.Checkpoint();<br> if( diffMemState.Difference( oldMemState, newMemState) )<br> {<br> TRACE( &quot;Memory leaked!\n&quot; );<br> }<br>#endif<br>(end of function)<br><br>Am I using this correctly?&nbsp;&nbsp;Please let me know.<br><br>Thank you<br>Nyjil
 
Dear Nujil,<br><br>&gt; Am I using this correctly?<br><br>Well if you are doing this:<br><br>Setup memory check<br>text = &quot;Goodbye&quot;;<br>UpdateData(FALSE);<br>check memory<br><br>Then no, you are not using it correctly<br><br>Hope this helps<br>-pete
 
Dear Palbano<br><br>What a relief to hear that I'm not doing it right. Thought I had a major memory leak because of it.&nbsp;&nbsp;I have been testing for memory leaks by the exact way that you described that I should not.&nbsp;&nbsp;Means something else is wrong with my program then.&nbsp;&nbsp;I appricate your time, and I will just have to continue to hunt down the problem then.<br><br>By the way, why is it that the memory check is coming up as a leak when I use it in the manner that you say is wrong?<br><br>Thanks yet again,<br><br>Nyjil
 
Dear nyjil,<br><br>From the MSDN documentation of CMemoryCheck:<br><br>************<br>CMemoryState provides a convenient way to detect memory leaks in your program. A “memory leak” occurs when memory for an object is allocated on the heap but not deallocated when it is no longer required.<br>************<br><br>That does not match the way you are using it because you are not deallocating the 'text' variable before performing the check. Also if I guess correctly, your variable 'text' is a stack based entity to begin with.<br><br>Hope this helps<br>-pete<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top