Solved

Dealocating memory in CEdit.

Posted on 1998-10-02
6
303 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

25 Experts available now in Live!

Get 1:1 Help Now