Version 0 of memory leak finding with memory trace

Updated 2004-10-22 15:26:49

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]