Tcl Source Code

View Ticket
Login
Ticket UUID: 1589794
Title: Memory leaks with TCL_THREADS
Type: Bug Version: obsolete: 8.3.4
Submitter: celine_thales Created on: 2006-11-03 09:28:21
Subsystem: 49. Threading Assigned To: vasiljevic
Priority: 5 Medium Severity:
Status: Closed Last Modified: 2007-06-30 21:02:31
Resolution: Wont Fix Closed By: vasiljevic
    Closed on: 2007-06-30 14:02:31
Description:
Hello,

I use tcl 8.3.4 on Win XP, and build with Visual C++ 6.0.
In need to use Tcl with thread implementation, and I
add TCL_THREADS compile option in settings of Tcl
project and calling project. Then I saw a lot of memory
leaks that appeared after call of Tcl_Finalize, at the
end of application in debug. I don't use it in a
console, so I use Tcl_Init, and Tcl_Finalize.

I needed to correct a bug in Tcl_FinalizeNotifier so
that it doesn't crash because pointer was NULL (Bug
#217982).

I added TclThread_Init just after Tcl_Init but don't
know if it's necessary.

I tried also with tcl 8.4.13 and this doesn't change
anything for memory leaks.

I tried to track leaks with TCL_MEM_DEBUG and call of
Tcl_DumpActiveMemory without success.

Is there anything special to do to be in multi
threading after setting TCL_THREADS?

Thank you for answering me because I begin to feel lost!

Celine
User Comments: vasiljevic added on 2007-06-30 21:02:31:
Logged In: YES 
user_id=95086
Originator: NO

Allright. The change you made was helping in 
the case Tcl_Init and Tcl_Finalize are not done
in the same thread. 

Normally Tcl_Init and Tcl_Finalize should be called
from the same thread (the main startup thread)
which is the first thread in your process. So the 
first thread should load and init the Tcl library and
it is this one thread that needs to finalize and unload
the Tcl library. You cannot delegate this to some other
thread.
Yep, this is not a sign of a good design and has
quite a few of side-effects like: you must somehow
terminate all threads using Tcl lib before your main
thread that loaded/inited Tcl lib exists.
But that's the way it is now; changing this would 
require a major library rewrite which is expensive. 
For the time being you should always pay attention
to load and finalize Tcl library in the same (preferably 
startup) thread.

Cheers
Zoran

celine_thales added on 2007-01-22 16:21:04:

File Added - 212462: TclEvent_c.txt

Logged In: YES 
user_id=1631532
Originator: YES

I joined diffTclEvent.jpg
File Added: TclEvent_c.txt

vasiljevic added on 2007-01-22 15:43:36:
Logged In: YES 
user_id=95086
Originator: NO

Can you tell me where exactly (post a code excerpt or a diff) you added 
TclFinalizeThreadData?

celine_thales added on 2007-01-22 15:30:23:
Logged In: YES 
user_id=1631532
Originator: YES

Hi, 
the solution I found at this moment was to add TclFinalizeThreadData in Tcl_Finalize and I modified the way my threads were called, because I realized that Tcl_Init was not necessary executed in the same calling thread as Tcl_Finalize. I have no more leaks now, but my solution might not be the right one.

Thank you to have taken my problem into account.

vasiljevic added on 2007-01-20 01:39:28:
Logged In: YES 
user_id=95086
Originator: NO

You are by no means lost. I'm slow but sure :-)
What you experience might be a problem in Tcl core
that badly handles std channels. I'm loking at that
at this moment.

celine_thales added on 2006-11-03 16:28:22:

File Added - 201253: LogTCL_MEM_DEBUG_thread.txt

Attachments: