Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2011-09-24
02:55 • Closed ticket [3392735fff]: Deep exit leaks one obj per stack level and local ... artifact: 8669fe1a48 user: dgp
2011-08-19
19:39 • Ticket [3392735fff]: 4 changes artifact: 2b507f6a0e user: dkf
19:19 • Ticket [3392735fff]: 4 changes artifact: 997e9440aa user: ferrieux
14:34 • Ticket [3392735fff]: 4 changes artifact: c16750bee5 user: mistachkin
2011-08-17
06:03 • Ticket [3392735fff]: 4 changes artifact: 505a4f6cdb user: dkf
2011-08-16
22:45 • New ticket [3392735fff]. artifact: 8cdcdf321d user: ferrieux

Deep [exit] leaks one obj per stack level and local var.

Closed / Duplicate3392735fffffffffffffffffffffffffffffffffCreated 15 years agoUpdated 15 years ago
Descriptionplain2011-08-16 22:45:42
With a -DPURIFY build, --enable-symbols:

proc deep n {
    if {$n<1} exit
    deep [expr {$n-1}]
}
deep [lindex $argv 0]

 valgrind --leak-check=yes ./tclsh deep.tcl 10


==2940== 27 (24 direct, 3 indirect) bytes in 1 blocks are definitely lost in loss record 33 of 50
==2940==    at 0x4005BDC: malloc (vg_replace_malloc.c:195)
==2940==    by 0x402EFB8: TclpAlloc (tclAlloc.c:705)
==2940==    by 0x403F188: Tcl_Alloc (tclCkalloc.c:1046)
==2940==    by 0x4107C18: Tcl_NewStringObj (tclStringObj.c:340)
==2940==    by 0x40E6CA4: NewNativeObj (tclMain.c:67)
==2940==    by 0x40E7148: Tcl_MainEx (tclMain.c:359)
==2940==    by 0x40E7977: Tcl_Main (tclMain.c:634)
==2940==    by 0x8048634: main (tclAppInit.c:84)
==2940== 
==2940== 216 bytes in 9 blocks are definitely lost in loss record 46 of 50
==2940==    at 0x4005BDC: malloc (vg_replace_malloc.c:195)
==2940==    by 0x402EFB8: TclpAlloc (tclAlloc.c:705)
==2940==    by 0x403F188: Tcl_Alloc (tclCkalloc.c:1046)
==2940==    by 0x40F0286: Tcl_NewWideIntObj (tclObj.c:2909)
==2940==    by 0x40ACFC1: TEBCresume (tclExecute.c:5128)
==2940==    by 0x40343EA: TclNRRunCallbacks (tclBasic.c:4311)
==2940==    by 0x4033EE0: Tcl_EvalObjv (tclBasic.c:4103)
==2940==    by 0x4035C32: TclEvalEx (tclBasic.c:5209)
==2940==    by 0x4035258: Tcl_EvalEx (tclBasic.c:4867)
==2940==    by 0x40DE382: Tcl_FSEvalFileEx (tclIOUtil.c:1751)
==2940==    by 0x40E72F6: Tcl_MainEx (tclMain.c:407)
==2940==    by 0x40E7977: Tcl_Main (tclMain.c:634)

Clearly, although full finalization has been requested, one extra obj remains for each stack level.
Experimenting with extra locals in the recursive proc seems to indicate one leak per local variable.
dkfplain2011-08-17 06:03:45
Why is it important to free all memory on [exit]? It just slows things down (especially with a large process) and the OS will reap all the resources. I see lots of programs in other languages which try to free everything on exit (rather than sticking to just important things) and the user experience is very negative.
mistachkinplain2011-08-19 14:34:08
It could be very important for embedding scenarios.  Also, it is generally a good idea to at least try cleaning up resources on exit, since some of those resources may indirectly refer to other resources that cannot be automatically reclaimed by the operating system upon process exit.
ferrieuxplain2011-08-19 19:19:39
Donal, again this special setup uses a forced-finalization-on-exit as a means, not an end :)
It is simply the only way to see leaks stand out. Once detected, many leaks prove to go beyond the exit sequence and can also be iterated in mid-life. Moreover, even teardown-time leaks can matter if they occur at the end of a thread (which is a repeatable condition). It is precisely the case here.
Now the forced finalization doesn 't occur in production builds; only in -DPURIFY  (see http://wiki.tcl.tk/28634 ) or when env var TCL_FINALIZE_ON_EXIT is set. So don't fear any slowdown in normal use.
dkfplain2011-08-19 19:39:42
Joe, when there are resources that must be cleaned up on exit, a specific exit handler should be registered to deal with them; there's a separate API for that. That API should not be (ab)used for doing memory cleanup though. (I differentiate with finalizing an interpreter; in that case there's a reasonable need to try to cleanse memory thoroughly. But it's not [exit].)

Alex, that's why it is important to say what you mean and not a proxy for it. The [exit] command should not be doing memory cleanup because of the issues I raised (other critical cleanup is the proper domain of an exit handler) but that doesn't mean that there is no need for testing finalization. (Suggestion: try ripping the rug out from under a sub-interp's feet using the master interpreter.)
dgpplain2011-09-24 02:55:14
allow_comments - 1


The key here is the use of [exit] in
the script.  Change "exit" to "return" and
all traces of leakage go away.

[exit] causes leaks.  That's ticket 3400944.
Log in (anonymous is fine) to add a comment.