Click to See Complete Forum and Search --> : "Damage after block..." on deleting a char* (VS2003)


Cabadam
July 25th, 2003, 11:14 PM
I have a doc/view app w/ a CRichEditView. In it, I define:
void CSyntaxView::ParseLine(long lineNum);

This function starts off by getting the text of the specified line into a CString):


CRichEditCtrl* ctrl = &GetRichEditCtrl();
int lineLen = ctrl->LineLength(lineNum);
char* buf = new char[lineLen];
ctrl->GetLine(lineNum, buf, lineLen);
CString line = buf;
line = line.Left(lineLen);
delete[] buf; ** Problem
buf = NULL;


However, when running in debug, I get an error on the delete line:
DAMAGE: after Normal block (#222) at 0x00337EF0.

I also tried the following:

CRichEditCtrl* ctrl = &GetRichEditCtrl();
int lineLen = ctrl->LineLength(lineNum);
CString line;
ctrl->GetLine(lineNum, line.GetBufferSetLength(lineLen), lineLen);
line.ReleaseBufferSetLength(lineLen);


That gave me the same error - but not until the function returned.

I have tried using both delete[] and just delete and I get the error either way. What am I doing wrong here???

mwilliamson
July 28th, 2003, 08:50 PM
This happens when you overwrite the bounds on an array. To fix your problem, change this line:

char* buf = new char[lineLen];

to

char* buf = new char[lineLen+1];