Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2004-07-16
00:44 • Closed ticket [983660ffff]: Leak of unshared literals at interp deletion plu... artifact: 251d826026 user: msofer
2004-07-15
19:20 • Ticket [983660ffff]: 4 changes artifact: 59fbf75639 user: msofer
19:20 • Open ticket [983660ffff]. artifact: f0e3664731 user: msofer
2004-07-09
01:49 • Closed ticket [983660ffff]. artifact: fc8df42df6 user: msofer
2004-07-08
05:47 • Ticket [983660ffff]: 4 changes artifact: eff31e1243 user: msofer
2004-07-06
05:48 • Ticket [983660ffff]: 7 changes artifact: 5d78c4cbef user: msofer
2004-07-05
17:25 • Ticket [983660ffff]: 4 changes artifact: c31779944c user: msofer
2004-07-04
21:41 • Ticket [983660ffff]: 4 changes artifact: 859da53e57 user: msofer
09:30 • Ticket [983660ffff]: 4 changes artifact: a4a5f794bf user: msofer
07:34 • Ticket [983660ffff]: 4 changes artifact: 52a884df16 user: msofer
07:19 • Ticket [983660ffff]: 4 changes artifact: 7cb360a7ce user: msofer
06:15 • Ticket [983660ffff]: 4 changes artifact: 9e55e03eb4 user: msofer
05:15 • Ticket [983660ffff]: 4 changes artifact: 3f90f1dc13 user: msofer
2004-07-03
02:12 • Ticket [983660ffff]: 5 changes artifact: 242f414aca user: dgp
02:05 • Ticket [983660ffff]: 4 changes artifact: c531284b86 user: dgp
2004-07-02
21:13 • Ticket [983660ffff]: 4 changes artifact: f67f0ff2de user: dgp
20:58 • Ticket [983660ffff]: 4 changes artifact: cb68d6c9a0 user: dgp
20:56 • Ticket [983660ffff]: 4 changes artifact: 5eef97156d user: dgp
05:06 • Ticket [983660ffff]: 5 changes artifact: a852cbe4ae user: msofer
04:25 • Ticket [983660ffff]: 1 change artifact: 7b78f80c6e user: hobbs
2004-07-01
20:31 • New ticket [983660ffff]. artifact: 5a2cd8306e user: pspjuth

Leak of unshared literals at interp deletion

Closed / Fixed983660ffffffffffffffffffffffffffffffffffCreated 22 years agoUpdated 22 years ago
Descriptionplain2004-07-01 20:31:40
If I repeat this:
  interp create testinterp
  catch {testinterp eval {lisx}}
  interp delete testinterp

memory increases with each iteration.
According to [memory info] 25 packets,
709 bytes, is lost each iteration.

If I replace "lisx" with "list" so no
error occurs, nothing leaks.

testinterp eval {catch lisx}

leaks as well

This is run with current CVS HEAD
on a WinXP machine.
hobbs Ticket [983660ffff]: 1 change [History diff]2004-07-02 04:25:37
msoferplain2004-07-02 05:06:18
Logged In: YES 
user_id=148712

Oh my, leak confirmed in current HEAD under linux, no
MEM_DEBUG (*). It is not present in 8.4.5.

(*) method: infinite loop 
 while 1 {
     interp create testinterp
     catch {testinterp eval {lisx}}
     interp delete testinterp
     after 100
 }
watched in top.
dgpplain2004-07-02 20:56:30
Logged In: YES 
user_id=80530


Change "lisx" to "error foo" and
the leak goes away.  Thus, I suspect
the [unknown]/auto-loader instead
of [return]/$::errorInfo.
dgpplain2004-07-02 20:58:57
Logged In: YES 
user_id=80530

Change back to "lisx"
and define [proc unknown args {}]
in testinterp and the leak also
goes away.
dgpplain2004-07-02 21:13:37
Logged In: YES 
user_id=80530


Define
[proc unknown args {return -code error foo}]
and the leak returns.

suspect something in the code that
calls [unknown] is not cleaning up
everything when [unknown] raises an error (?)
dgpplain2004-07-03 02:05:12
Logged In: YES 
user_id=80530

Somehow the INST_RETURN
command is involved.  Still
investiagting...
dgpplain2004-07-03 02:12:16
Logged In: YES 
user_id=80530

The [unknown] appears to be
a sideshow.  This script should
verify this is simply a leak in
INST_RETURN execution:

while 1 {
  interp create i
  i eval {proc foo {} {return -code break}
  catch {i eval foo}
  interp delete i
}
msoferplain2004-07-04 05:15:56
Logged In: YES 
user_id=148712

That script (missing a '}') leaks too
msoferplain2004-07-04 06:15:03
Logged In: YES 
user_id=148712

The leak, according to valgrind:

48 bytes in 2 blocks are definitely lost in loss record 1 of 2
   at 0x3C01F40D: malloc (vg_replace_malloc.c:105)
   by 0x80FF82C: TclpAlloc (tclAlloc.c:677)
   by 0x809B274: Tcl_Alloc (tclCkalloc.c:1001)
   by 0x80715BF: Tcl_DuplicateObj (tclObj.c:862)
   by 0x80A7357: TclMergeReturnOptions (tclCmdMZ.c:968)
   by 0x80B3CD3: TclCompileReturnCmd (tclCompCmds.c:2520)
   by 0x80BB16E: TclCompileScript (tclCompile.c:1072)
   by 0x80BA540: TclSetByteCodeFromAny (tclCompile.c:401)
   by 0x80BA748: SetByteCodeFromAny (tclCompile.c:499)
   by 0x807B46C: TclProcCompileProc (tclProc.c:1216)
msoferplain2004-07-04 07:19:17
Logged In: YES 
user_id=148712

Comments that are probably relevant:

(1) in tclLiteral.c (l 448, TclAddLiteralObj): the literalis
*not* added to the literal tables, only to the literal array

(2) in tclCompile.c (l 661, TclCleanupByteCode): if the
interp is marked as deleted, it will already have cleaned up
all literals so we refrain from doing it again

(3) in tclBasic.c (l 1112, DeleteInterpProc): interp
deletion cleans up all literals in the literal table

So it looks like the returnOptions added to the literal
array (but not to the table) at line 2593 in tclCompCmds.c
(TclCompileReturnCmd) is bypassed by the cleanup process.
msoferplain2004-07-04 07:34:11
Logged In: YES 
user_id=148712

Indeed - inserting
  i eval {rename foo {}}
before deleting i removes the leak.
Note that this is a more general bug, the returnOpts is just
a trigger. Every non-shared literal in a proc body will be
leaked if the proc is not explicitly deleted before the interp. 
Amazing that this hasn't been detected in usage of
precompiled proc bodies (proComp)!
msoferplain2004-07-04 09:30:48
Logged In: YES 
user_id=148712

Mmhh .. diagnose not 100% accurate yet
msoferplain2004-07-04 21:41:47
Logged In: YES 
user_id=148712

The bug is apparently in the freeing of non-shared literal
objects; note that TclCompileReturnCmd is the only caller of
TclAddLiteralObj in the core, and that TclHideLiteral is not
used at all in the core.
msoferplain2004-07-05 17:25:20
Logged In: YES 
user_id=148712

Rewinding, back to square 1: the bug does seem to be in the
handling of the return options, as storing them as a shared
literal does not plug the leak. This was tested by replacing
TclAddLiteralObj with TclRegisterNewLiteral.

Somehow, the proc body is not being cleaned up properly on
interp deletion. This is determined by noticing that either of
  i eval {rename  foo {}}
  i eval {proc foo {} {}}
before deleting the interp plugs the leak. The leak does not
appear with other literals.
msoferplain2004-07-06 05:48:01
Logged In: YES 
user_id=148712

Oh boy - finally found it. DeleteInterpProc was releasing
the interp's handle before tearing down the global
namespace, so that TclCleanupByteCode was being called with
a NULL interp. This prevented the proper freeing of literals.

The current solution may have an impact on interp deletion
speed; leaving open so that I remember to look for more a
more efficient solution.
msoferplain2004-07-08 05:47:14
Logged In: YES 
user_id=148712

The performance issue is: 
* before the patch, the interp deletion loops through the
interp's literal table releasing all literals in the interp
(in a suboptimal manner, but just one loop and with no hashing)
* after the patch, every proc in the interp will loop
through its own literal array releasing the literals - and
doing a hash lookup for each one. No literals remain for the
overall interp deletion loop.

A possible solution is to register unshared literals in the
interp's table with refCount -1 (to signal that they should
not be shared). Note that this is actually specified in the
comments (tclInt.h line 921), but not done in the code.
msoferplain2004-07-09 01:49:31
Logged In: YES 
user_id=148712

Fix modified: at interp deletion, each bytecodeobject
releases its literal references but does not update the
dying literal table; in this way, there are is no hashing
involved. 
When TclDeleteLiteralTable is called, the only remaining
references are those in the table itself.
msofer Open ticket [983660ffff]. [History diff]2004-07-15 19:20:29
msoferplain2004-07-15 19:20:30
Logged In: YES 
user_id=148712

Reopening: the fix is buggy, compile-12.1 is failing on the
current HEAD (compiled with --disable-shared
--enable-symbols=all, threaded and uthreaded builds). 

The new logic at interp deletion is bypassing the special
code for empty bytecodes (see also bug 467523).
msoferplain2004-07-16 00:44:54
Logged In: YES 
user_id=148712

Closing this ticket, bug 467523 that had been rsurrected is
now solved.
Log in (anonymous is fine) to add a comment.