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.
$ cd <To your hw5 directory> $ cp /cse/courses/cse303/05au/hw5/dmem/dmem.h . $ cp /cse/courses/cse303/05au/hw5/dmem/libdmem.a .
$ tar zxvf dmem-cygwin-0.1.bin.tar.gz
makefile
in your homework5 directory and modify like this:makefile in your hw5 directory.
dllistdriver
, find$(CC) -o dllistdriver dllistdriver.o dllist/dllist.oline and append
-L. -ldmem
at the end of the line. The line will look like this:$(CC) -o dllistdriver dllistdriver.o dllist/dllist.o -L. -ldmem
hw5
, find$(CC) -o hw5 $^ -lfl -L. -llexerline and append
-ldmem
at the end of the line. The line will look like this:$(CC) -o hw5 $^ -lfl -L. -llexer -ldmem
btdriver
, find$(CC) -o btdriver btdriver.o bt/bt.o dllist/dllist.oline and append
-L. -ldmem
at the end of the line. The line will look like this:$(CC) -o btdriver btdriver.o bt/bt.o dllist/dllist.o -L. -ldmem
MOREFLAGS
like this.$ cd <To your hw5 directory> $ export MOREFLAGS="-DDMEM -include `pwd`/dmem.h"
$ cd <To your hw5 directory> $ make clean $ make
$ unset MOREFLAGS $ cd <To your hw5 directory> $ make clean $ make
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:
stderr
will be used by default.Option | Meaning | Default action |
---|---|---|
DUMP_MEMORY | Dump the content of leaked memory | Not dump memory |
NO_LEAKS | Only report overall memory leackage statistics | Report detailed memory leaks |
NO_FINALIZE | Do not finalize dmem library itself at the end of execution | Finalize dmem library at the end of execution |
For example, to enable several options simultaneously, you might do something like this:
$ export DMEM_OPTION="NO_LEAKS:NO_FINALIZE"In this case, the dmem library will generate only overall statistics and will not finalize itself.
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 leaksI 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