[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
problems.

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-
siasm.

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>
#endif

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.

Greetings,
Aragorn (implementor of Imagica MUD)

------------
ACMD(do_debug)
{
#ifndef DMALLOC
send_to_char("Sorry, the debugging library is disabled.\r\n", ch);
#else
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);
        return;
        }

skip_spaces(&argument);
half_chop(argument, arg1, arg2);

if (!strcmp(arg1, "log"))
        {
        dmalloc_log_unfreed();
        }
if (!strcmp(arg1, "mark"))
        {
        mark = dmalloc_mark();
        dmalloc_message("-------------------------------");
        dmalloc_message("Marked mark : %d", mark);
        }
if (!strcmp(arg1, "changed"))
        {
        if (mark == 0)
                {
                send_to_char("There is no mark set.\r\n", ch);
                return;
                }
        dmalloc_message("Changed log for mark : %d", mark);
        dmalloc_log_changed(mark, 1, 0, 1);
        dmalloc_message("-------------------------------");
        }
if (!strcmp(arg1, "stats"))
        {
        dmalloc_log_stats();
        }
if (!strcmp(arg1, "shut"))
        {
        dmalloc_shutdown();
        }
if (!strcmp(arg1, "map"))
        {
        dmalloc_log_heap_map();
        }
if (!strcmp(arg1, "check"))
        {
        if (*arg2)
                {
                tmp = arg2;
                skip_spaces(&tmp);
                dmalloc_verify(tmp);
                }
        else    {
                dmalloc_verify(NULL);
                }
        }
#endif
}


--
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