[debugging] dmalloc library usage

From: Martijn Schoemaker (martijn@osp.nl)
Date: 08/09/02

Hi all,

It has been some time I posted to this list, but as
I see more and more people trying to track down me-
mory errors I would like to point you all to the fo-
llowing extremely handy tool called : dmalloc.

Dmalloc is a library against which you link your
circle binary. You have to make small code changes
(you just need to add an include file to your
list of includes in every .c file) and you can
use this library to debug those annoying memory

What does it do ? Basically the dmalloc library wraps
all your free, malloc, calloc, etc. calls and tracks
it like a heap-controller. This means you can at
any time log which pieces of memory you've got allo-
cated and even better, from which .c-file:line com-
bination it was allocated! It will also track attempts
to free NULL pointers and pointers that were already
freed. It even contains the possibility to debug
nasty fence-post errors which show you when you try
to access previously freed memory. I personally found
loads of problems using this library, hence my enthou-

Where do I get it ? The complete site can be found
at : http://dmalloc.com.

How can I use it ? Well as previously mentioned, you
just insert statements like the following at the END
of your include list in EVERY .c file you use :

#ifdef DMALLOC
#include <dmalloc.h>

Then add a -DDMALLOC to your compile directive list,
and make sure you use a -ldmalloc to link in the
library when neccesary. And that's it!

Well, almost. Now you can just add an extra wiz
command (see the code below) to do all kinds
of needo stuff. For instance : Say you have a
memory leak somewhere in OLC code (ehm, yes
you have!) Using the library with the code below
you just type : debug mark, do the action you want
to test on memory leak (f.i. oedit and exiting
without saving) and then do a : debug changed.
This will send all non-freed pointers to the
logfile you configured for dmalloc and you
know where to look to fix the problem. (you
configure the library by setting environment
variables before you start your circle process).

On the website is more in-depth details on how
the library works, but I can tell you one thing,
I removed loads of memory leaks/corruption pro-
bems with this library. Once you get the hang of
it, you will find it is an invaluable asset to
your MUD building tools.

Aragorn (implementor of Imagica MUD)

#ifndef DMALLOC
send_to_char("Sorry, the debugging library is disabled.\r\n", ch);
static  unsigned long   mark = 0;
char    arg1[MAX_INPUT_LENGTH];
char    arg2[MAX_INPUT_LENGTH];
char    *tmp;

if (!*argument)
        send_to_char("Usage : debug <log|stats>\r\n", ch);

half_chop(argument, arg1, arg2);

if (!strcmp(arg1, "log"))
if (!strcmp(arg1, "mark"))
        mark = dmalloc_mark();
        dmalloc_message("Marked mark : %d", mark);
if (!strcmp(arg1, "changed"))
        if (mark == 0)
                send_to_char("There is no mark set.\r\n", ch);
        dmalloc_message("Changed log for mark : %d", mark);
        dmalloc_log_changed(mark, 1, 0, 1);
if (!strcmp(arg1, "stats"))
if (!strcmp(arg1, "shut"))
if (!strcmp(arg1, "map"))
if (!strcmp(arg1, "check"))
        if (*arg2)
                tmp = arg2;
        else    {

You have reached the end of the message.
Press [t] to go to the top of this message, or [c] to close it.

   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   | Newbie List:  http://groups.yahoo.com/group/circle-newbies/   |

This archive was generated by hypermail 2b30 : 06/25/03 PDT