Go Back   EQEmulator Home > EQEmulator Forums > Archives > OpenEQ > OpenEQ::Development

OpenEQ::Development Development discussion for OpenEQ. Do not post for support.

Reply
 
Thread Tools Display Modes
  #1  
Old 09-30-2004, 11:53 AM
daeken_bb
Discordant
 
Join Date: Mar 2003
Location: Chambersburg, PA
Posts: 469
Default Debugging malloc/free/realloc implimentation

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:
Code:
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:
Code:
#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)
__________________
Keep me unemployed and working on OpenEQ, PM me about donating

Check out my deviantART page at http://daeken.deviantart.com/
Reply With Quote
  #2  
Old 10-09-2004, 12:26 AM
smogo
Discordant
 
Join Date: Jan 2004
Location: 47
Posts: 339
Default

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)
__________________
EQEMu Quest Repository is down until something new :(
Reply With Quote
  #3  
Old 10-12-2004, 06:40 PM
Doodman's Avatar
Doodman
Developer
 
Join Date: Aug 2003
Posts: 246
Default

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.
Reply With Quote
  #4  
Old 10-12-2004, 06:43 PM
Doodman's Avatar
Doodman
Developer
 
Join Date: Aug 2003
Posts: 246
Default

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.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

   

All times are GMT -4. The time now is 02:42 PM.


 

Everquest is a registered trademark of Daybreak Game Company LLC.
EQEmulator is not associated or affiliated in any way with Daybreak Game Company LLC.
Except where otherwise noted, this site is licensed under a Creative Commons License.
       
Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Template by Bluepearl Design and vBulletin Templates - Ver3.3