Tk Source Code

View Ticket
Login
Ticket UUID: 703a7fdda790e26f7f286bdbace3818e0bf4a1c6
Title: Every Tk_Init registers another thread exit handler
Type: Bug Version: 9.0.5
Submitter: serhiy.storchaka Created on: 2026-11-16 23:17:42
Subsystem: 99. Other Assigned To: nobody
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2026-11-17 11:16:38
Resolution: Fixed Closed By: nemethi
    Closed on: 2026-11-17 11:16:38
Description:

Initialize() in generic/tkWindow.c calls TkCreateThreadExitHandler(DeleteWindowsExitProc, tsdPtr) each time an interpreter loads Tk. The handlers are freed only at thread exit, so every Tk_Init in a thread, e.g. every new tkinter.Tk(), leaks 24 bytes. Found with Itcl's leak test, which loads Tk into a new child interpreter for each run.

Fix: delete the previous registration first:

TkDeleteThreadExitHandler(DeleteWindowsExitProc, tsdPtr);
TkCreateThreadExitHandler(DeleteWindowsExitProc, tsdPtr);
The same code is in 8.6, 9.0 and main.

User Comments: nemethi (claiming to be Csaba Nemethi) added on 2026-11-17 11:16:38:

Serhiy, many thanks! Merged into main, core-9-0-branch, and core-8-6-branch by commits [2e51198f], [72f3c9ef], and [fb629f35].


serhiy.storchaka added on 2026-11-16 23:18:56:

Proposed fix in branch tk-init-exit-handler-once. Tested with memory-debug builds of Tcl and Tk 9.0: with several interpreters loading Tk, one handler stays registered per thread instead of one per interpreter, and the windows of all interpreters are still destroyed at exit. window.test, main.test, winfo.test, frame.test and send.test pass on X11.