Hey, I'm writing a C program that uses a whole lot of malloc and free commands. I've run into a snag where partway through execution, it fails on a free command. However I've checked thoroughly by printing out memory addresses as they are allocated and freed, and this memory is allocated with malloc and used successfully before I attempt to use free on it.
Luckilly I always used this to call free:
So for debugging I just commented out the line: free (pointer);Code:void safefree (void *pointer) { if (pointer) { #ifdef DEBUG fprintf (debugOut, "%p: safefree\n", pointer); fflush (debugOut); #endif free (pointer); } }
However, then I found out that the program was still hanging on free, but within the fflush command! And this time, as I'm writing this post, it's the printf command causing the problem. The problem is always in SubBlock_merge_prev, which is nested deep within the free command (nothing I have anything to do with).
The call stack according to CodeWarrior (8.3) debugger:
I have a temporary fix, which is the following:Code:0x77E8141A( kernel32.dll ) mainCRTStartup main printf __pformatter __FileWrite fwrite __fwrite __flush_buffer __write_file __msl_write free __pool_free deallocate_from_fixed_pools deallocate_from_var_pools Block_link SubBlock_merge_prev
However this makes the program eat a TON of memory.Code:void free (void *ptr) {}
I tried using Visual Studio, and it no longer crashes if I comment the free line in safefree. So at least standard libraries aren't crashing there.However, with my free uncommented, I get "DAMAGE: after Normal block (#57) at 0x0032FC18." I'm not sure if this is Visual Studio's gentle way of telling me I'm freeing memory twice, but the space should be allocated correctly with malloc so I'm not sure what the problem is.
Any ideas?



LinkBack URL
About LinkBacks
However, with my free uncommented, I get "DAMAGE: after Normal block (#57) at 0x0032FC18." I'm not sure if this is Visual Studio's gentle way of telling me I'm freeing memory twice, but the space should be allocated correctly with malloc so I'm not sure what the problem is.



Hopefully with the help of my debuggers I'll get it eventually. Thanks both of you!
Thanks for your help everyone! It did turn out to be overflow (off by 1).