Version 1 of memory leak finding with memory trace

Updated 2004-10-22 16:54:31 by AK

When configured with --enable-symbols=mem, Tcl includes a [memory] command that can be used to debug memory problems. The [memory trace] command dumps a record of all allocations and frees of memory.

When searching for a memory leak, that's a haystack of data to search for the needle of the leak. The following script pulls out the useful information:

 proc ckalloc {addr size file line} {
    variable store
    variable i
    if {[llength [array names store *,$addr,$size]]} {
        puts stderr "Double alloc!: $addr $size $file $line"
    }
    set store([incr i],$addr,$size) "$file $line"
 }
 proc ckfree {addr size file line} {
    variable store
    array unset store *,$addr,$size
 }
 set i 0
 source [lindex $argv 0]

 foreach name [array names store] {
    foreach {i addr size} [split $name ,] {break}
    lappend out [list $i "LEAK: $addr $size $store($name)"]
 }
 puts [join [lsort -index 0 -integer $out] \n]

---

AK notes: * How can a double allocation for the same address happen ? Also, if it can happen, is the size of the struct not irrelevant for the collision ? Right now the script will not detect a collision on the same address with different sizes.

* ckfree should be extended to check if there is data for the adress available. Because if not then we have found a double-free.