"Damage after block..." on deleting a char* (VS2003)

A

Adam Clauss

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 have tried using both delete[] and just delete and I get the error either
way. What am I doing wrong here???

If it would help here, is there an easier way to get the specified line into
a CString?
 
A

Adam Clauss

Hmm... I ended up reading that after I posted this.
Whats odd is that four wasn't the magic number. Tried it with + 4 to the
buffer and it still happened.

I haven't yet found which number it starts working for - but I kind of
randomly tested + 20 and it worked ;)

I'll go through and figure out what the cutoff number is.

--
Adam Clauss
(e-mail address removed)
David Lowndes said:
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.

Adam,

I think you need to consider this part of the documentation:

"Remarks
The copied line does not contain a terminating null character.

Note Because the first word of the buffer stores the number of
characters to be copied, make sure that your buffer is at least 4
bytes long.
"

...so try increasing the new'ed buffer by 4 bytes and see if that
prevents the memory error.

Dave
 
Joined
Oct 13, 2006
Messages
1
Reaction score
0
Thank you...

Just thought I would say thanks for your answer.
I was running into a simular problem and your answer made me take a look at the code again. I had written past the end of the array in my effort to put the '\0'.

All for the want of a "+1"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top