Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-10-12
17:12 • Closed ticket [1710825fff]: "Still reachable" memory leaks in Tcl I/O ... artifact: b52b2e85db user: serhiy.storchaka
17:08 • Ticket [3400944fff] exit creates leaks status still Open with 3 other chan... artifact: aea4f0dafc user: serhiy.storchaka
2011-08-31
20:20 • Ticket [3400944fff]: 4 changes artifact: e4deab9ff9 user: foxcruiser
05:23 • Ticket [3400944fff]: 4 changes artifact: 5d4b4196a3 user: ferrieux
02:33 • Ticket [3400944fff]: 4 changes artifact: a28c656a91 user: dgp
01:30 • Ticket [3400944fff]: 5 changes artifact: 87fc915d0d user: dgp
01:25 • Ticket [3400944fff]: 1 change artifact: 4f30212f33 user: dgp
2011-08-30
18:25 • New ticket [3400944fff]. artifact: b3029448b2 user: dgp

[exit] creates leaks

Open3400944fffffffffffffffffffffffffffffffffCreated 15 years agoUpdated 51 days ago
Descriptionplain2011-08-30 18:25:04
Even using the special support for valgrind
(see 2919042) testing any script with [exit]
in it is going to show leaks of things that would
be freed by the natural unwinding that the [exit]
prevents.  For example,

$ cat leak.tcl
namespace eval foo exit

$ valgrind --leak-check=yes ./tclsh leak.tcl
...
==13495== 36 bytes in 3 blocks are definitely lost in loss record 40 of 50
==13495==    at 0x4005903: malloc (vg_replace_malloc.c:195)
==13495==    by 0x80DF060: TclpAlloc (tclAlloc.c:705)
==13495==    by 0x80EF8C0: Tcl_Alloc (tclCkalloc.c:1046)
==13495==    by 0x80E630A: TclArgumentEnter (tclBasic.c:5473)
==13495==    by 0x80E5E98: TclEvalEx (tclBasic.c:5213)
==13495==    by 0x80E54BE: Tcl_EvalEx (tclBasic.c:4872)
==13495==    by 0x806D584: Tcl_FSEvalFileEx (tclIOUtil.c:1751)
==13495==    by 0x8072F0F: Tcl_MainEx (tclMain.c:407)
==13495==    by 0x8073573: Tcl_Main (tclMain.c:634)
==13495==    by 0x805465B: main (tclAppInit.c:84)
==13495==
==13495== 201 (48 direct, 153 indirect) bytes in 2 blocks are definitely lost in loss record 47 of 50
==13495==    at 0x4005903: malloc (vg_replace_malloc.c:195)
==13495==    by 0x80DF060: TclpAlloc (tclAlloc.c:705)
==13495==    by 0x80EF8C0: Tcl_Alloc (tclCkalloc.c:1046)
==13495==    by 0x808ED07: Tcl_NewStringObj (tclStringObj.c:340)
==13495==    by 0x80817A3: TclSubstTokens (tclParse.c:2337)
==13495==    by 0x80E5A63: TclEvalEx (tclBasic.c:5101)
==13495==    by 0x80E54BE: Tcl_EvalEx (tclBasic.c:4872)
==13495==    by 0x806D584: Tcl_FSEvalFileEx (tclIOUtil.c:1751)
==13495==    by 0x8072F0F: Tcl_MainEx (tclMain.c:407)
==13495==    by 0x8073573: Tcl_Main (tclMain.c:634)
==13495==    by 0x805465B: main (tclAppInit.c:84)

The same issue is present using [testthread exit].
dgp Ticket [3400944fff]: 1 change [History diff]2011-08-31 01:25:27
dgpplain2011-08-31 01:30:05
This is relevant to the `make valgrind` target.
It takes the test suite to be a suitable body of
code to cover seeking leaks.  So long as the
test suite includes use of [testthread exit] and
similar things, though, leaks will be reported,
likely false alarms.

I suppose a constraint could be used to work
around the matter.  Hoping for better ideas.
dgpplain2011-08-31 02:33:46
I see that the Thread package docs explicitly
warn against the [thread::exit] command in favor
of a preserve/release system.  Lessons the
testthread command could adopt, or the test suite
could migrat from [testthread] to [package require Thread].
ferrieuxplain2011-08-31 05:23:05
This is similar to 3392735. Another facet :/
foxcruiserplain2011-08-31 20:20:06
We got the same issue with [exit] when memory debugging XOTcl/NSF based on our test suite  (with an internal refcounter and valgrind). We ended up with falling back to a graceful unwind instead of Tcl_Exit(), for the scope of test scripts:

 proc graceFullExit {{statuscode "1"}} {
      array set map {1 ok -1 error}
      set errorcode $map($statuscode)
      interp alias {} ::exit {}
      interp expose {} exit; 
      return -code $errorcode -level [expr {[info level]-1}] "Test was exited with code $statuscode"
    }

and then, before evaluating a test body:

 interp hide {} exit; 
 interp alias {} ::exit {} ::graceFullExit

works fine for us ...
serhiy.storchakawiki2026-10-12 17:08:05

Still true in 9.1: [exit] does not unwind the stack, so whatever the unwinding would free is reported by valgrind.

A possible solution is to make [exit] unwind the stack, like interp cancel -unwind, and exit the process when the unwinding is complete. catch could not stop it, but finally clauses would run, so the script could clean up other resources, as with Python's SystemExit exception. This is a visible change in semantics and needs a TIP.

The test suite problem was addressed as suggested here: tests which exit a thread in mid-operation have the notValgrind constraint, and [testthread exit] is no longer used in tests.

Log in (anonymous is fine) to add a comment.