memory leak finding with memory trace

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,*]]} {
        puts stderr "Double alloc at $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 ?

It should not. Probably impossible, but I'd rather have the data tell me than assume.

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

Good point. Script modified.

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

Problem with that is a ckfree of memory allocated before the [memory trace on] command runs is OK. Anyone willing to make the mods to deal with that is welcome to do so.

ak: Should be possible: Just remember if there has been any allocation for this addr/size. If yes, then it is a double-free. Otherwise it can be a free where the alloc was not recorded.