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:
It should not. Probably impossible, but I'd rather have the data tell me than assume.
Good point. Script modified.
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.