Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-10-22
17:19 • Ticket [2172279fff] Crash when calling Tcl_FinalizeThread() status still Clo... artifact: 3bf31c8c12 user: serhiy.storchaka
2026-10-12
18:33 • Closed ticket [2172279fff]. artifact: a4779a2ebd user: serhiy.storchaka
2014-02-21
19:09 • Ticket [2172279fff]: 1 change artifact: 9652dbb383 user: jan.nijtmans
2008-10-16
23:56 • Ticket [2172279fff]: 4 changes artifact: 90e765c040 user: nobody
16:46 • New ticket [2172279fff]. artifact: dc4087c3ad user: nobody

Crash when calling Tcl_FinalizeThread()

Closed / Invalid2172279fffffffffffffffffffffffffffffffffCreated 18 years agoUpdated 41 days ago
Descriptionplain2008-10-16 16:46:59
I want to write an application that is able to start and stop a tcl interpreter on demand.
But there seems to be a problem  - when calling Tcl_FinalizeThread the application crashes !

Tcl_FinalizeThread() does not accept any parameters, so if I want to finalize the Tcl thread I have to call it from the Tcl thread (using the event queue with Tcl_ThreadQueueEvent).
When doing this a crash occurs.

Tcl_FinalizeThread cleans up many data structures (queueMutex,...). But this data sttructures are neccessary to execute the following code section in Tcl_ServiceEvent. Because these data structures have been freed, Tcl tries to access a ptr 0xdddddddd.

I do not think that there is a valid context to call Tcl_FinalizeThread, so please fix the bug.


1. callstack

 tcl84.dll!Tcl_MutexLock(Tcl_Mutex_ * * mutexPtr=0x03922cac)  Line 500 + 0xc bytesC
 tcl84.dll!Tcl_ServiceEvent(int flags=0xfffffffd)  Line 641 + 0xc bytesC
>tcl84.dll!Tcl_DoOneEvent(int flags=0xfffffffd)  Line 945 + 0x9 bytesC
 HmiTclSupport.dll!TclThreadMainLoop()  Line 735 + 0x1a bytesC++
 tcl84.dll!Tcl_Main(int argc=0xffffffff, char * * argv=0x03fa15e0, int (Tcl_Interp *)* appInitProc=0x03f984d0)  Line 444 + 0x8 bytesC


2. the location the error occurs

int
Tcl_ServiceEvent(flags)
    int flags;/* Indicates what events should be processed.
 * May be any combination of TCL_WINDOW_EVENTS
 * TCL_FILE_EVENTS, TCL_TIMER_EVENTS, or other
 * flags defined elsewhere.  Events not
 * matching this will be skipped for processing
 * later. */
{
    Tcl_Event *evPtr, *prevPtr;
    Tcl_EventProc *proc;
    int result;
    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

    /*
     * Asynchronous event handlers are considered to be the highest
     * priority events, and so must be invoked before we process events
     * on the event queue.
     */
    
    if (Tcl_AsyncReady()) {
(void) Tcl_AsyncInvoke((Tcl_Interp *) NULL, 0);
return 1;
    }

    /*
     * No event flags is equivalent to TCL_ALL_EVENTS.
     */
    
    if ((flags & TCL_ALL_EVENTS) == 0) {
flags |= TCL_ALL_EVENTS;
    }

    /*
     * Loop through all the events in the queue until we find one
     * that can actually be handled.
     */

    Tcl_MutexLock(&(tsdPtr->queueMutex));
    for (evPtr = tsdPtr->firstEventPtr; evPtr != NULL;
 evPtr = evPtr->nextPtr) {
/*
 * Call the handler for the event.  If it actually handles the
 * event then free the storage for the event.  There are two
 * tricky things here, both stemming from the fact that the event
 * code may be re-entered while servicing the event:
 *
 * 1. Set the "proc" field to NULL.  This is a signal to ourselves
 *    that we shouldn't reexecute the handler if the event loop
 *    is re-entered.
 * 2. When freeing the event, must search the queue again from the
 *    front to find it.  This is because the event queue could
 *    change almost arbitrarily while handling the event, so we
 *    can't depend on pointers found now still being valid when
 *    the handler returns.
 */

proc = evPtr->proc;
if (proc == NULL) {
    continue;
}
evPtr->proc = NULL;

/*
 * Release the lock before calling the event procedure.  This
 * allows other threads to post events if we enter a recursive
 * event loop in this thread.  Note that we are making the assumption
 * that if the proc returns 0, the event is still in the list.
 */

Tcl_MutexUnlock(&(tsdPtr->queueMutex));
result = (*proc)(evPtr, flags);
-> crash::
-> crash::
-> crash::
-> crash::
-> crash::
Tcl_MutexLock(&(tsdPtr->queueMutex));
-> crash::
-> crash::
-> crash::
-> crash::



if (result) {
    /*
     * The event was processed, so remove it from the queue.
     */
[email protected]plain2008-10-16 23:56:53
it seems that TclFinalizeThreadData() called by Tcl_FinalizeThread() causes the problem
jan.nijtmans Ticket [2172279fff]: 1 change [History diff]2014-02-21 19:09:19
serhiy.storchakawiki2026-10-12 18:33:54

Tcl_FinalizeThread() frees all per-thread Tcl data, including the event queue, so it cannot be called while Tcl is still running in that thread, for example from an event handler: Tcl_ServiceEvent() uses the event queue after the handler returns. Leave the event loop first (e.g. set a flag in the handler and check it in the loop) and call Tcl_FinalizeThread() after that.

serhiy.storchaka Ticket [2172279fff]: 3 changes [History diff]2026-10-22 17:19:24
Log in (anonymous is fine) to add a comment.