Artifacts Associated With Ticket 2172279
Ticket change [dc4087c3ad] (rid 108107) by nobody on 2008-10-16 16:46:59:
- assignee initialized to: "nijtmans"
- cmimetype initialized to: "text/plain"
- comment initialized to:
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. */ - foundin initialized to: "obsolete: 8.4.19"
- is_private initialized to: "0"
- priority initialized to: "5 Medium"
- resolution initialized to: "None"
- status initialized to: "Open"
- submitter initialized to: "nobody"
- subsystem initialized to: "02. Event Loops"
- title initialized to: "Crash when calling Tcl_FinalizeThread()"
- type initialized to: "Bug"
Ticket change [90e765c040] (rid 120533) by nobody on 2008-10-16 23:56:53:
- icomment:
it seems that TclFinalizeThreadData() called by Tcl_FinalizeThread() causes the problem
- login: "[email protected]"
- mimetype: "text/plain"
- username: "[email protected]"
- icomment:
Ticket change [9652dbb383] (rid 137005) by jan.nijtmans on 2014-02-21 19:09:19:
- assignee changed to: "jan.nijtmans"
Ticket change [a4779a2ebd] (rid 257783) by serhiy.storchaka on 2026-10-12 18:33:54:
- icomment:
<code>Tcl_FinalizeThread()</code> 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: <code>Tcl_ServiceEvent()</code> 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 <code>Tcl_FinalizeThread()</code> after that.
- login: "serhiy.storchaka"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "Invalid"
- status changed to: "Closed"
- icomment:
Ticket change [3bf31c8c12] (rid 259897) by serhiy.storchaka on 2026-10-22 17:19:24:
- closedate changed to: "2461326.273543252"
- closer changed to: "serhiy.storchaka"
- login: "serhiy.storchaka"