Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2004-07-16
00:42 • Ticket [467523ffff] memory leak in namespace eval foo {} status still Clos... artifact: d87cf33cda user: msofer
2001-10-12
23:15 • Ticket [467523ffff]: 4 changes artifact: bf59bce541 user: msofer
23:15 • Add attachment leak.tests to ticket [467523ffff] artifact: ee873120b7 user: msofer
23:15 • Ticket [467523ffff] memory leak in namespace eval foo {} status still Clos... artifact: 88f5445418 user: msofer
05:37 • Closed ticket [467523ffff]. artifact: 26938966fa user: msofer
2001-10-11
20:13 • Ticket [467523ffff]: 4 changes artifact: 668adb0b03 user: msofer
20:13 • Ticket [467523ffff]: 4 changes artifact: f962350dc1 user: msofer
20:13 • Add attachment leak.patch to ticket [467523ffff] artifact: 7de57e41d1 user: msofer
2001-10-10
00:03 • Ticket [467523ffff] memory leak in namespace eval foo {} status still Open... artifact: b74ae19a25 user: msofer
2001-10-09
23:38 • Ticket [467523ffff]: 4 changes artifact: fa0602ecd5 user: rbrunner
23:02 • Open ticket [467523ffff]. artifact: f039c5dedd user: msofer
22:39 • Ticket [467523ffff]: 6 changes artifact: 57a0d3f6fe user: rbrunner
2001-10-06
08:05 • Closed ticket [467523ffff]. artifact: b0c128ca24 user: msofer
07:02 • Ticket [467523ffff]: 4 changes artifact: 673078b502 user: msofer
2001-10-05
20:37 • Ticket [467523ffff]: 5 changes artifact: 139a71d4b2 user: msofer
02:27 • Ticket [467523ffff]: 4 changes artifact: 53627a9298 user: rbrunner
2001-10-04
02:12 • Ticket [467523ffff]: 4 changes artifact: 77832976e8 user: msofer
00:16 • Ticket [467523ffff]: 4 changes artifact: c3eb1817d0 user: msofer
00:16 • Ticket [467523ffff]: 4 changes artifact: 578b7d1670 user: msofer
2001-10-03
23:43 • Ticket [467523ffff]: 5 changes artifact: b4352300a4 user: hobbs
15:47 • New ticket [467523ffff]. artifact: 9ca7facb0a user: rbrunner

memory leak in [namespace eval foo {}]

Closed / Fixed467523ffffffffffffffffffffffffffffffffffCreated 25 years agoUpdated 22 years ago
Descriptionplain2001-10-03 15:47:27
The following code leaks (in every Tcl version):

  interp create foo 
  foo eval {
      namespace eval bar {}
  }
  interp delete foo

Note: 

  interp create foo 
  foo eval {
      namespace eval bar {\#}
  }
  interp delete foo

does NOT leak.

Unfortunately I didn't really track down the problem, 
but the call 

  result = Tcl_EvalObjEx(interp, objv[3], 0);

in generic/tclNamesp.c line 2955 (tcl8.4.a3), which is 
called with an empty string in objv[3] seems to cause 
the leak. Changing this code to 

  if (strlen(Tcl_GetString(objv[3])))
    result = Tcl_EvalObjEx(interp, objv[3], 0);
  else
    result = TCL_OK;

fixes the symptoms, but certainly not the problem.
(Sorry for not having a proper fix.)
hobbsplain2001-10-03 23:43:49
Logged In: YES 
user_id=72656

Confirmed in 8.3.3.
msoferplain2001-10-04 00:16:55
Logged In: YES 
user_id=148712

I think the leak should occur at any evaluation of {}, ie,
also for
    proc a {} {}
    a

As I see it, the special handling of "bytecodes with empty
source" in Tcl_EvalObjEx is at fault. I enclose a small
patch that should correct this.

Ronnie, Jeff: does this fix the bug?
msoferplain2001-10-04 00:16:57
File Added - 11528: 467523.patch
msoferplain2001-10-04 02:12:50
Logged In: YES 
user_id=148712

No, that wasn't it ...
rbrunnerplain2001-10-05 02:27:06
Logged In: YES 
user_id=205230

Some further observations:
Any recursive call to Tcl_EvalObjEx with an empty string 
object to eval leaks:

  time {time {}}
  time {switch 1 1 {}}
  switch 1 1 {time {}}

In addition I found out, that these calls leak, whenever 
the innermost empty code matches the Literal that is added 
in TclCompile.c line 1041 (tcl8.4a3):

  TclRegisterLiteral(envPtr, "", 0, /*onHeap*/ 0)

Setting this to

  TclRegisterLiteral(envPtr, "  ", 2, /*onHeap*/ 0)

makes 

  time {time {  }}

leak. It seems that this equality increments the empty 
codes object's refCount (objPtr in Tcl_evalObjEx) to be too 
high -> it is not cleaned up properly.
msoferplain2001-10-05 20:37:05
Logged In: YES 
user_id=148712

The problem (I think) is that the object referred to by the
literal "" in the slave interp is never freed, as it is
self-referential: the bytecode pushes itself on the stack,
so that the bytecode object always has refCount >=1 - it
appears in its own local literal table. 

The code leaks 164 bytes: 24 for the object, 128 for its
internal rep as a bytecode, 12 for the handle to the
interpreter (it would be freed by CleanupByteCode when the
object is freed).

One possible solution I am exploring is to be more careful
about sharing literals. The solution to this bug along this
path could conceivably also fix [Bug 458361] in a roundabout
way.
msoferplain2001-10-06 07:02:03
File Added - 11620: leak.patch

Logged In: YES 
user_id=148712

The attached leak.patch to generic/tclLiteral.c
(TclReleaseLiteral) solves the issue.
msoferplain2001-10-06 08:05:05
Logged In: YES 
user_id=148712

Patch committed to HEAD and core-8-3-1-branch
rbrunnerplain2001-10-09 22:39:42
Logged In: YES 
user_id=205230

Miguel: did you only patch tclLiteral.c? Compiling with the 
latest version of this file (1.9, same as 1.8.2.1) tcl8.4a3 
crashes running the following code:

for {set a 0} {$a < 10} {incr a} {
    namespace eval bar {}
    namespace delete bar
}

my tclsh dumps core with

  file = ./../generic/tclExecute.c, line = 663
  Trying to increment refCount of previously disposed 
object.
  Abort (core dumped)
msoferplain2001-10-09 23:02:53
Logged In: YES 
user_id=148712

Ronnie: Yes, the patch only adds one loc to tclLiteral.c

I do not see any problems with that code in either 8.4a3,
8.4a4 or the 8-3-1-branch code - your script does not
trigger a dump on my setup.

Could you help me reproduce the dump? I'd need to know 
  - configure and compile flags
  - how you are running the script: tclsh? tkcon? from a
file?
  - anything else?

Thanks
rbrunnerplain2001-10-09 23:38:20
Logged In: YES 
user_id=205230

Here a complete log of what I'm doing. Note: running the 
code in an interactive tclsh works (no dump) and not 
changing the MEM_DEBUG_FLAGS in Makefile *ALSO* works (no 
dump). Let me know whether you need the tclConfig.sh or any 
other additional info ...

> uname -a
SunOS mlt 5.6 Generic_105181-26 sun4u sparc SUNW,Ultra-250
> tar xzf tcl8.4a3.tar.gz
> cd tcl8.4a3/generic
>

# apply patch to tclLiteral.c (just copy 1.9 over 1.8)

> cd ../unix
./configure
>

# change Makefile to memory debug
    #MEM_DEBUG_FLAGS =
    MEM_DEBUG_FLAGS = -DTCL_MEM_DEBUG

> make
> setenv LD_LIBRARY_PATH .
>

# write a mini script foo: vi foo
    #!./tclsh
    for {set a 0} {$a < 10} {incr a} {
namespace eval bar {}
namespace delete bar
    }

> chmod +x foo
> ./foo
file = ./../generic/tclExecute.c, line = 663
Trying to increment refCount of previously disposed object.
Abort (core dumped)
>
msoferplain2001-10-10 00:03:01
Logged In: YES 
user_id=148712

Dump confirmed on linux; I am removing the patch from HEAD
and core-8-3-1-branch
msoferplain2001-10-11 20:13:14
Logged In: YES 
user_id=148712

I do think I have a correct patch now - the special handling
of self-referencing bytecodes in TclreleaseLiteral
(tclLiteral.c) was being done too early (entryPtr->refCount
== 2 at function entry) and removing two global table
references in one go (setting entryPtr->refCount to 0) while
decreasing the object's corresponding refCount by just one.

My earlier patch corrected this by removing a reference to
the object too - as Ronnie discovered, in some situations
this caused the object to be accessed after it was removed.

This patch restores "normal handling": each call to
TclReleaseLiteral removes at most one reference in the
global table, and the special code for self-referencing
bytecodes is only used at the last pass.
msoferplain2001-10-11 20:13:15
File Deleted - 11528: 



File Added - 11851: leak.patch
msoferplain2001-10-12 05:37:17
Logged In: YES 
user_id=148712

Patch committed to HEAD and core-8-3-1-branch
msoferplain2001-10-12 23:15:28
Logged In: YES 
user_id=148712

Added tests in tests/compile.test - they are only effective
if tcl was compiled with TCL_MEM_DEBUG
msoferplain2001-10-12 23:15:29
File Added - 11886: leak.tests
msoferplain2004-07-16 00:42:21
Logged In: YES 
user_id=148712

This bug reappeared with the (faulty) fix of bug #983660 (in
tcl8.5a2), and was fixed using a different strategy.

The previous strategy was to have special code in
TclReleaseLiteral to handle the self-references generated by
empty scripts. The new approach avoids the self-reference
altogether, by having empty scripts return an unshared literal.
Log in (anonymous is fine) to add a comment.

Attachments: