Solved

Dealocating memory in CEdit.

Posted on 1998-10-02
6
305 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 50 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mixString challenge 36 113
maven archtype selection in eclipse 1 53
Window placement 17 77
Capture logon name 13 48
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now