Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Dealocating memory in CEdit.

Posted on 1998-10-02
6
Medium Priority
?
344 Views
Last Modified: 2013-11-20
I'm using a CEditView like this:
( MyCEditView->GetEditCtrl() ).SetWindowText( myBuffer );
if I "free( myBuffer )" after that, there is a debug exception.
Do I have to dealocate myBuffer?
When?
0
Comment
Question by:wau
  • 2
  • 2
  • 2
6 Comments
 

Expert Comment

by:umarama
ID: 1322874
how have you defined & initialized mybuffer ? Can you give the relevant code ?

0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 200 total points
ID: 1322875
>> Do I have to dealocate myBuffer?

Only if you allocated it

If you allocated using new then use delete

If you allocated using malloc then use free

If you didn't allocate (simply declared on assigned a variable) then you do NOT need to free it.

If you are required to free it, you can do it anytime after you stop using myBuffer in your program, for example if you never use myBuffer after SetWindowText, free it then.


0
 

Author Comment

by:wau
ID: 1322876
Ok. I supose that if I allocated myBuffer, I have to free it.
I allocate it by char * myBuffer=(char*)malloc( length );
the I fill and finally I
( MyCEditView->GetEditCtrl() ).SetWindowText( myBuffer );
But, If i free(myBuffer); after SetWindowText, I have an exception.
What really means SetWindowText? The Cedit objet allocate his own buffer and copy, or points to myBuffer?
if after SetWindowText, I call another time
( MyCEditView->GetEditCtrl() ).SetWindowText( myanotherBuffer );
what happens with myBuffer?
Thanks very much.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Expert Comment

by:umarama
ID: 1322877
SetWindowText expects you to provide either a CString object or a NULL terminated string. IT DOES NOT ALLOCATE ANY BUFFER FOR YOU. You allocate, set the text and pass it to SetWindowText. The system simply displays your text. You are responsible for deallocating your the string's memory.

The problem you have mentioned occurs when the string is not NULL terminated. The SetWindowText uses CString conversion routine which has a problem of overwriting allocated memory area blocks. The free() throws an exception if the allocation signature is missing.

My suggestion would be to look into your code to check the method of your setting the display string, i.e. myBuffer. Make sure you are not doing memory copy but string copy with appropriate NULL termination.

0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1322878
SetWindowText API takes a copy of the buffer supplied, you don't need to keep the buffer afterwards

SetWindowText MFC version also expects a pointer to a buffer and simply calls SetWindowText API.  Again you don't need to keep the buffer afterwards.

In the case of passing a CString to SetWindowText MFC the CString class includes an operator to cast to a pointer which deals with the copying issue.  Again you don't need to keep the buffer afterwards.

In all cases the buffer must be a NUL terminated string.  (CString will normally do this for you when converting to LPCTSTR)

Looking at the fragment of code you give, it looks like you're buffer is one to short (you don't allow an extra character for the NUL)

char * myBuffer=(char*)malloc( length + 1  );  /* +1 for the extra NUL character */
0
 

Author Comment

by:wau
ID: 1322879
ok. now. answers2000.
Thanks very much.
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question