Solved

Dealocating memory in CEdit.

Posted on 1998-10-02
6
309 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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to retreive data from .TFS file exported by DVR 8 198
WinWaitActive parameters 12 31
matchUp  challenge 9 108
"Black Box" Testing of Control System Software 2 51
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 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