error while "free"ing memory

P

prashantkhoje

hi ppl,
i am developing an C application in Microsoft visual C++ 6.0. i get
following error while running it in debug mode:


/* here's the error message */

program: my_application.exe
DAMAGE: after Block number (#41) at 0x002F51C0

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

the number in brackets varies every time i run the program.

the program is somewhat like this:
/* here's the code */
int main(void)
{
char *ptr=NULL;
..
..
..
ptr = (char*)calloc(ResReq.max_context_memory_use, sizeof(char));

// then i do my stuff here using memory from ptr, that works pretty
fine
// time to free the memory
free(ptr); /* HERE APPEARS THE ERROR :( */
return 0;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

please let me know why the error occurs and most importantly how to get
rid of it.
thank you in advance.

prashant
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

hi ppl,
i am developing an C application in Microsoft visual C++ 6.0. i get
following error while running it in debug mode:


/* here's the error message */

program: my_application.exe
DAMAGE: after Block number (#41) at 0x002F51C0

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

the number in brackets varies every time i run the program.

the program is somewhat like this:
/* here's the code */
int main(void)
{
char *ptr=NULL;
.
.
.
ptr = (char*)calloc(ResReq.max_context_memory_use, sizeof(char));
// then i do my stuff here using memory from ptr, that works pretty
fine

Likely it doesn't work, and that's the cause of the error. e.g. you
write past the bounds of an array/buffer.
Come up with a minimal testcase that shows the behavior.
// time to free the memory
free(ptr); /* HERE APPEARS THE ERROR :( */
Which error ?
 
P

pemo

hi ppl,
i am developing an C application in Microsoft visual C++ 6.0. i get
following error while running it in debug mode:


/* here's the error message */

program: my_application.exe
DAMAGE: after Block number (#41) at 0x002F51C0

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

the number in brackets varies every time i run the program.

the program is somewhat like this:
/* here's the code */
int main(void)
{
char *ptr=NULL;
.
.
.
ptr = (char*)calloc(ResReq.max_context_memory_use, sizeof(char));

// then i do my stuff here using memory from ptr, that works pretty
fine
// time to free the memory
free(ptr); /* HERE APPEARS THE ERROR :( */
return 0;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

please let me know why the error occurs and most importantly how to
get rid of it.
thank you in advance.

// then i do my stuff here using memory from ptr, that works pretty
fine

Or so you think. It'll be in this code that you're probably stepping out of
the memory you allocated via calloc(). Can you post the code that comes
before the free() call?
 
K

Kenneth Brody

hi ppl,
i am developing an C application in Microsoft visual C++ 6.0. i get
following error while running it in debug mode:

/* here's the error message */

program: my_application.exe
DAMAGE: after Block number (#41) at 0x002F51C0

That is MSVC's "you have corrupted the dynamic memory heap" message.
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

the number in brackets varies every time i run the program.

the program is somewhat like this:
/* here's the code */
int main(void)
{
char *ptr=NULL;
.
.
.
ptr = (char*)calloc(ResReq.max_context_memory_use, sizeof(char));

As pointed out already, don't cast the return from malloc/calloc/etc
as it is not required, and will mask other errors.

(Also, note that "sizeof(char)" is 1 by definition.)
// then i do my stuff here using memory from ptr, that works pretty fine

Obviously not. :)
// time to free the memory
free(ptr); /* HERE APPEARS THE ERROR :( */
return 0;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

please let me know why the error occurs and most importantly how to get
rid of it.

Don't overrun the end of any of your buffers.

Where is the code that actually modifies the buffer to which ptr points?
thank you in advance.

Note that it is entirely possible that some _other_ buffer was overrun,
and it just wasn't detected until the "free(ptr)" was executed. Do you
have any other malloc's?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
J

Jordan Abel

hi ppl,
i am developing an C application in Microsoft visual C++ 6.0. i get
following error while running it in debug mode:

the program is somewhat like this:
/* here's the code */
int main(void)
{
char *ptr=NULL;
.
.
.
ptr = (char*)calloc(ResReq.max_context_memory_use, sizeof(char));

// then i do my stuff here using memory from ptr, that works pretty
// fine

Clearly, it doesn't. You're doing something that screws up the value of
ptr, or overrunning the buffer it points at [in either direction]
// time to free the memory
free(ptr); /* HERE APPEARS THE ERROR :( */
return 0;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

please let me know why the error occurs and most importantly how to get
rid of it.

The error lies in the part of code that you commented out.
 
N

Neil

hi ppl,
i am developing an C application in Microsoft visual C++ 6.0. i get
following error while running it in debug mode:


/* here's the error message */

program: my_application.exe
DAMAGE: after Block number (#41) at 0x002F51C0

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

the number in brackets varies every time i run the program.

the program is somewhat like this:
/* here's the code */
int main(void)
{
char *ptr=NULL;
.
.
.
ptr = (char*)calloc(ResReq.max_context_memory_use, sizeof(char));

// then i do my stuff here using memory from ptr, that works pretty
fine
// time to free the memory
free(ptr); /* HERE APPEARS THE ERROR :( */
return 0;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

please let me know why the error occurs and most importantly how to get
rid of it.
thank you in advance.

prashant
No Error checking? What did caloc() return?
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top