Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2002-08-29
16:38 • Closed ticket [597936ffff]: leak in new thread allocator plus 6 other change... artifact: 17364aafbb user: vasiljevic
2002-08-24
21:16 • Open ticket [597936ffff]. artifact: c22553dae2 user: msofer
08:31 • Closed ticket [597936ffff]. artifact: dde546dafe user: msofer
07:29 • Ticket [597936ffff]: 4 changes artifact: 54e1d77862 user: msofer
05:14 • Ticket [597936ffff]: 4 changes artifact: d3bd6280bc user: vasiljevic
05:01 • Ticket [597936ffff]: 4 changes artifact: 3393795718 user: vasiljevic
2002-08-23
19:35 • Ticket [597936ffff]: 4 changes artifact: cbf474e191 user: msofer
2002-08-22
22:16 • Ticket [597936ffff]: 4 changes artifact: 0df9f67f28 user: andreas_kupries
22:12 • Ticket [597936ffff]: 4 changes artifact: e350485c22 user: msofer
22:03 • Ticket [597936ffff]: 4 changes artifact: 36a4d24dfd user: msofer
21:47 • Ticket [597936ffff]: 4 changes artifact: 6fd80f387c user: andreas_kupries
21:10 • Ticket [597936ffff]: 4 changes artifact: bca1ad062c user: msofer
2002-08-20
20:39 • New ticket [597936ffff]. artifact: b07ac7d139 user: msofer

leak in new thread allocator

Closed / Works For Me597936ffffffffffffffffffffffffffffffffffCreated 24 years agoUpdated 24 years ago
Descriptionplain2002-08-20 20:39:58
The following script runs forever. With the new Tcl_Obj
allocator (USE_THREAD_ALLOC) its memory grows slowly
but steadily; all seems fine with the standard allocator.

  lappend auto_path /CVS/thread/unix
  package require Thread
  proc spawn {} {
      global i
      after 10 spawn
      thread::create "puts [incr i]"
      flush stdout
  }
  
  set i 0
  spawn
  vwait forever
msoferplain2002-08-22 21:10:18
Logged In: YES 
user_id=148712

Note that this has nothing to do with [after], the following
shows the same behaviour:

lappend auto_path /CVS/thread/unix
package require Thread
set i 0
while 1 {
    thread::create "puts [incr i]"
}
andreas_kupriesplain2002-08-22 21:47:56
Logged In: YES 
user_id=75003

Hm. Could it be that the pool of objects associated with a 
thread is not freed after a thread is done. Or maybe they are 
just shuffled around into the global pool. In both situations we 
would have an ever-growing list of free objects. Either as true 
leak, or simply because we keep creating thread-object-pools 
but never using the allocated objects.
msoferplain2002-08-22 22:03:02
Logged In: YES 
user_id=148712

Mmhh ... seems to be related to tclsh not cleaning up
properly on exit - in particular, the encoding stuff. I
could detect where the leak is with valgrind; the leaked mem
was created from

==7357==    at 0x400467C4: malloc (vg_clientfuncs.c:100)
==7357==    by 0x805D430: TclThreadAllocObj (in
/CVS/tcl_SF_clean/unix/tclsh)
==7357==    by 0x8055F87: Tcl_NewObj (in
/CVS/tcl_SF_clean/unix/tclsh)
==7357==    by 0x80657C8: TclpInitLibraryPath (in
/CVS/tcl_SF_clean/unix/tclsh)
==7357==    by 0x8089693: TclFindEncodings (in
/CVS/tcl_SF_clean/unix/tclsh)
==7357==    by 0x8087C28: Tcl_FindExecutable (in
/CVS/tcl_SF_clean/unix/tclsh)
==7357==    by 0x805508A: Tcl_Main (in
/CVS/tcl_SF_clean/unix/tclsh)
==7357==    by 0x8054EE7: main (in /CVS/tcl_SF_clean/unix/tclsh)
==7357==    by 0x4029E627: __libc_start_main
(../sysdeps/generic/libc-start.c:129)
==7357==    by 0x8054DE1: strcpy@@GLIBC_2.0 (in
/CVS/tcl_SF_clean/unix/tclsh)
msoferplain2002-08-22 22:12:25
Logged In: YES 
user_id=148712

Aarghh ... looking at the wrong place, disregard previous
comment! Leak is from

==7385==    at 0x400467C4: malloc (vg_clientfuncs.c:100)
==7385==    by 0x805D430: TclThreadAllocObj (in
/CVS/tcl_SF_clean/unix/tclsh)
==7385==    by 0x8055F87: Tcl_NewObj (in
/CVS/tcl_SF_clean/unix/tclsh)
==7385==    by 0x8066EDB: Tcl_CreateInterp (in
/CVS/tcl_SF_clean/unix/tclsh)
==7385==    by 0x4023F0D8: NewThread (in
/CVS/thread/unix/libthread2.4.so)
==7385==    by 0x40250166: thread_wrapper (vg_libpthread.c:521)
==7385==    by 0x4004B7E0: do__apply_in_new_thread_bogusRA
(vg_scheduler.c:2023)
andreas_kupriesplain2002-08-22 22:16:55
Logged In: YES 
user_id=75003

This does seem to match my explanation. Objects allocated 
during construction of the thread are not freed when tearing it 
down. Possibly just moved over to the global pool.
msoferplain2002-08-23 19:35:50
Logged In: YES 
user_id=148712

With an fprintf here and an fprintf there, eeah eeah oh...

All objects should be moved to the global pool by the
TclFreeAllocCache exit handler. However, some objects in
each thread (in this example, 286 of them) are not being
moved over at thread exit, and are leaked. 

Apparently TclFreeAllocCache is being called before all
objects have been decrRefCount'ed out of existence? From
'man pthread_key_create':
"The order in which destructor functions are called at
thread termination  time is unspecified."
vasiljevicplain2002-08-24 05:01:29
Logged In: YES 
user_id=95086

Simple. Here's the CVS diff: 
 
Index: tclObj.c 
=================================================================== 
RCS file: /cvsroot/tcl/tcl/generic/tclObj.c,v 
retrieving revision 1.39 
diff -r1.39 tclObj.c 
686c686,688 
< 
--- 
> #ifdef USE_THREAD_ALLOC 
>     TclThreadFreeObj(objPtr); 
> #else 
698a701 
> #endif /* USE_THREAD_ALLOC */ 
 
It was a simple matter of not calling the right release. 
So the memory was allocated with the new and  
released with the old allocator.  
This is not a thread issue per-se.  
 
Cheers 
Zoran
vasiljevicplain2002-08-24 05:14:10
Logged In: YES 
user_id=95086

Unfortunately, this was not the only issue. Apparently,  
there are some others, which I'm still looking at. 
So, stay tuned.
msoferplain2002-08-24 07:29:52
Logged In: YES 
user_id=148712

I think it is the Tcl_DecrRefCount macro in tcl.h (line
791). If we want to keep binary compatibility, we shouldn't
modify that macro - rather redefine TclFreeObj() to do the
right thing.
msoferplain2002-08-24 08:31:27
Logged In: YES 
user_id=148712

OK, that was it. Zoran's patch was unlocking a mutex which
hadn't been locked - fixed #ifdef's and committed to HEAD.
msoferplain2002-08-24 21:16:50
Logged In: YES 
user_id=148712

Oops ... I should get more sleep indeed! Zoran's patch did
solve the issue, I had just applied it wrongly ...
So: reopening, whatever else Zoran saw is not the error I
was getting from his patch. Sorry Zoran :(
vasiljevicplain2002-08-29 16:38:05
Logged In: YES 
user_id=95086

After careful (memdebug/purify) testing it seems that  
this one can be closed safely.
Log in (anonymous is fine) to add a comment.