CSE 303 Homework 5 - Debugging memory leaks

Preface

I have written a small library to help you debugging memory leaks. When your program terminates, it will tell you about apparent memory leaks (memory allocated but not freed), with some detailed information like the location of allocation, size and content. It is small and fast, since it does nearly nothing compared to the other commercial quality debugging and profiling tools, so can be used even when running your program against the full data set. Even though it doesn't have the full functionality of production tools, I'm sure that this library will help give you some idea to how to debug your program.

Note: This program is segmentation fault prone. If you try to free memory that wasn't allocated, it will generate error message with specific location and exit gracefully. But if your program has a bug causing it to seg fault because of a corrupted heap, the library probably makes the situation worse. So if you are debugging a segmentation fault, I recommend that you not use this library and instead try the other way such as gdb with core dump.

How to install?

  1. If you don't have hw#5 version 1.1, please download and install it first
  2. You can download the library and header file from the following links:
  3. Edit makefile in your homework5 directory and modify like this:
  4. Set environment variable MOREFLAGS like this.
    $ cd <To your hw5 directory>
    $ export MOREFLAGS="-DDMEM -include `pwd`/dmem.h"
    
  5. Now you are ready to use the library with your program. Rebuild your program to let the dmem library work.
    $ cd <To your hw5 directory>
    $ make clean
    $ make
    
  6. If you want to build your program without the library, type following commands:
    $ unset MOREFLAGS
    $ cd <To your hw5 directory>
    $ make clean
    $ make
    
  7. Enjoy debugging! :-)

How to use it?

You can turn on the library by specifying -DDMEM in MOREFLAGS variable. Also, you can turn off the library by dropping -DDMEM from the variable.
Whenever you change the variable, you must rebuild your program to reflect the change. Say make clean and then make.

DMEM library receives some options through these environment variables:

Here is a sample output from my Part B implementation:

$ ./hw5 ./data www.nytimes.com

Done: 0 pages, 0 words, 0 links total
memory leaks: 1 memory chunks have not been freed
0x004b0300(22 bytes): allocated at lexer.c(100)
    ./.c.y.g.d.r.i.v.e./.z./.h.w.5./.d.a.t.a./00
Total: 22 bytes memory leaks
I used DUMP_MEMORY option to get more clear idea about which memory has not been freed. Every printable character is printed with '.' prefix. Non-printable character like '\0' will be printed as 2 digit hexa-decimal form. It says that this 22 bytes leaking memory(address 0x004b0300) allocated at line 100 in lexer.c and the content is null-terminated /cygdrive/z/hw5/data/ string.

Written by YongChul Kwon