Solved

Dealocating memory in CEdit.

Posted on 1998-10-02
6
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Change owner from userA to server Local Administrators 7 74
conditional code and condition difference 9 87
either24  challenge 19 110
JQuery serialize and unserialize 8 271
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.

751 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