473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

error while "free"ing memory

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

Feb 8 '06 #1
5 2023
pr***********@gmail.com wrote:
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)); Remember to include <stdlib.h> Casting the return value of malloc isn't
needed.
// 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 ?
return 0;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

Feb 8 '06 #2
pr***********@gmail.com wrote:
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?
--
==============
Not a pedant
==============
Feb 8 '06 #3
pr***********@gmail.com wrote:

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:Th*************@gmail.com>
Feb 8 '06 #4
On 2006-02-08, pr***********@gmail.com <pr***********@gmail.com> wrote:
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.
thank you in advance.

Feb 8 '06 #5
pr***********@gmail.com wrote:
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?
Feb 11 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike Krasnik | last post by:
Hello all, I've been messing with checking memory leaks last time, and it looks like some of the memory was not freed due to the fact "library's default allocators keep free memory in a pool for...
5
by: Scott Brady Drummonds | last post by:
Hi, everyone, A coworker and I have been pondering a memory allocation problem that we're having with a very large process. Our joint research has led us to the conclusion that we may have to...
13
by: Mars | last post by:
Why this error occurs?? typedef char *string; int main(void) { string str; str="ajsdklfajsdf"; printf("%s\n",str);
5
by: Michael | last post by:
How can I determine a users PC Total system memory and memory available or free at the time of my program loading. Thanks
42
by: Martin Jørgensen | last post by:
Hi, I'm trying to move a matlab program into c language. For those who knows matlab, this is the line I want to program in c: hx(1:nx,1:ny) = 0; % nx=10, ny=10 It works on a 2-dimensional...
16
by: Stef Mientki | last post by:
If I create a large array of data or class, how do I destroy it (when not needed anymore) ? Assign it to an empty list ? thanks, Stef Mientki
2
by: =?Utf-8?B?UmFscGggSQ==?= | last post by:
OK, Dell inspirion 9300 - 100 gb hd partitioned into 3 drives C: OS 10 gb D: Programs 20 gb E: Data 70 GB Page Files 0 on C: 4092 on D: 3070 on E:
84
by: aarklon | last post by:
Hi all, I found an interesting article here:- http://en.wikipedia.org/wiki/Criticism_of_the_C_programming_language well what do you guys think of this article....??? Is it constructive...
0
by: Steve Holden | last post by:
Hank @ITGroup wrote: It doesn't really make that much sense to watch memory usage as you have been doing. Your first test case appears to trigger a specific pathology, where the memory allocator...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.