PDA

View Full Version : Debugging malloc/free/realloc implimentation


daeken_bb
09-30-2004, 11:53 AM
I wrote this wrapper to allow for easier debugging of allocation/reallocation/freeing issues, as well as ease of tracking down memory leaks.

To use, simply build safe_mem.c into your application and include safe_mem.h from every file. An easy way to do this is to include safe_mem.h from a file that's included from all your source (e.g. internal type declarations). Everything else is done for you automagickly, and no further modifications must be made to your code.

If the precompiler constant SAFE_MEM is declared then the implimentation is enabled and calls to malloc() and realloc() will print an error if a null pointer is generated from either. If the precompiler constant SAFE_MEM_DEBUG is declared (in addition to SAFE_MEM) then every call to malloc, realloc, and free will print debug output in addition to the null pointer error checking.

Source is available for download at http://home.archshadow.com/~daeken/safe_mem.c and http://home.archshadow.com/~daeken/safe_mem.h, as well as below.

safe_mem.c:

void *srealloc(void *o, int s, int line_number, char *filename) {
void *p;
#ifdef SAFE_MEM_DEBUG
printf("SRealloc: Attempting to reallocate %i bytes to %p. (%s:%i)\n", size, o, filename, line_number);
#endif

p = (void *) realloc(o, size);

if(!p)
printf("SRealloc: Could not reallocate %i bytes to %p! (%s:%i)\n", size, o, filename, line_number);
#ifdef SAFE_MEM_DEBUG
else
printf("SRealloc: Successfully reallocated %i bytes to pointer %p (Originally %p). (%s:%i)\n", size, p, o, filename, line_number);
#endif
return p;
}

void *smalloc(int size, int line_number, char *filename) {
void *p;
#ifdef SAFE_MEM_DEBUG
printf("SMalloc: Attempting to allocate %i bytes. (%s:%i)\n", size, filename, line_number);
#endif

p = (void *) malloc(size);

if(!p)
printf("SMalloc: Could not allocate %i bytes! (%s:%i)\n", size, filename, line_number);
#ifdef SAFE_MEM_DEBUG
else
printf("SMalloc: Successfully allocated %i bytes to pointer %p. (%s:%i)\n", size, p, filename, line_number);
#endif
return p;
}

void sfree(void *p, int line_number, char *filename) {
#ifdef SAFE_MEM_DEBUG
printf("SFree: Freeing pointer %p. (%s:%i)\n", p, filename, line_number);
#endif

free(p);

#ifdef SAFE_MEM_DEBUG
printf("SFree: Successfully freed pointer %p. (%s:%i)\n", p, filename, line_number);
#endif
}


safe_mem.h:

#ifndef __EQCLIENT_SAFE_MEM_H_
#define __EQCLIENT_SAFE_MEM_H_

#ifdef SAFE_MEM

void *srealloc(void *p, int s, int line_number, char *filename);
void *smalloc(int size, int line_number, char *filename);
void sfree(void *p, int line_number, char *filename);

#define realloc(p, bytes) (smalloc((p), (bytes), __LINE__, __FILE__))
#define malloc(bytes) (smalloc((bytes), __LINE__, __FILE__))
#define free(p) (sfree((p), __LINE__, __FILE__))

#endif

#endif


Enjoy.

Happy Hacking,
Lord Daeken M. BlackBlade
(Cody Brocious)

smogo
10-09-2004, 12:26 AM
As for finding memory leaks ... best is not to have them at all, and use automated garbage collection.

There is a widely used GC by Boehm and al. http://www.hpl.hp.com/personal/Hans_Boehm/gc/

It's conservative, that means it requires no changes to the C or C++ code, and is mostly accurate.

Peformance and memory usage are kept pretty small (and let me consider any arguing on this pointless)

Doodman
10-12-2004, 06:40 PM
Just an FYI, you can make you code work without having to change existing code.

1) Do it like electric fences does (or use electric fence: emerge efence) and write a static library where you write your own malloc, calloc, realloc that call _malloc, _calloc, _realloc respectively. (IIRC on the naming convention of the internal functions). And then just re-link with your new library and you'll get you malloc, calloc, realloc functions. I've done this before, btw, I know it works.
2) Do the above but make it a shared libary and set LD_PRELOAD=mylib.so and no relinking is needed, so it could be used on -any- executable not just your own.

Just some tips.

Doodman
10-12-2004, 06:43 PM
Also, what I did in mine was to keep track of what was allocated so that freeing unallocated memory would spit out an warning. I also hooked in to _at_exit() and the like to have it report all memory that it still showed allocated but never freed when the application exitted. Kinda handy.

I'll see if I can scare up the code that I wrote.