Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Heap Error

Status
Not open for further replies.

CMR

Programmer
Apr 30, 2002
76
AU
Hello all

I'm writing a Console application that supports MFC. Basically, it scans through a text file and updates a datbase with the data in the file. Pretty simple stuff - but I have a strange problem which I can only assume is related to memory management.
I'm getting a heap error when I try to use CString::Right() - HEAP: Free Heap block 4b5d408 modified at 4b5d47c after it was freed.

I'm sure it isn't CString that's the problem here as when I comment this line out the error moves elsewhere. (it just seems to move to the next instruction that involves allocating memory).
The weird thing is, the program will run for a bit and then fail - i.e. it will process the first three or so lines of my text file but then gives a "user breakpoint" error and the debug window will show the heap error.

Any ideas?

Thanks in advance for any help

CMR
 
It menas that you have written beyind some bounds in allocated memory.
Greetings,
Rick
 
Found the problem:

I had two CString objects, one local and one global. I was making the global one equal to the local one which waas causing problems when the local function went out of scope.
Apparently, CString "=" doesn't copy each character as you might expect it to; it creates a pointer to the original CString for the copy so when the original goes out of scope it causes memory allocation problems.

I'll try to avoid making that mistake again....

CMR
 
Actually i can't get the problem. if possible paste the sample code for explaining.

i tried like this

#include "afx.h"
CString xx;
void fn1();
void main(void){
fn1();
if(xx.IsEmpty())
cout<<&quot;Empty&quot;;
else
cout<<xx;
}

void fn1(){
CString x = &quot;std&quot;;
xx = x;
}
 

I don't think it's as simple as that. The problem seems to occur not when you copy the string, but when you next try to allocate memory - i.e. when you use &quot;new&quot; of something that dynamically allocates memory.
I wasn't able to tell exactly which instruction it was falling over at (it was buried deep in a .lib file I think) but when I declared all my CStrings as local rather than some local, some global it fixed the problem.

Thanks for taking the time to look at it anyway

CMR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top