Tk Source Code

View Ticket
Login
Ticket UUID: a58cda72fec0f1565db08e6adaadf0cf037fdb2a
Title: Threaded Window Creation Leads to Geometry Corruption and Crashes windows 10
Type: Bug Version: 9.1a0
Submitter: anonymous Created on: 2025-07-24 05:36:11
Subsystem: 68. Win Window Operations Assigned To: nobody
Priority: 5 Medium Severity: Minor
Status: Open Last Modified: 2025-07-24 09:03:25
Resolution: None Closed By: nobody
    Closed on:
Description:
Summary:
In Tk 9.1a0, creating Tk windows on windows platform in multiple threads reveals two problems:

1. Windows sometimes receive corrupted geometry values, such as `792x229+0+1499287696`.
2. In these cases, the window never appears, even when `wm geometry` or `wm deiconify` is used.
3. At higher thread counts (around 450–500), Tk crashes with a read access violation due to a NULL dereference in `TkWinGetDrawableDC()`.

This bad geom behavior nor the crash does not occur in Tk 8.6.13

Crash Stack Trace (at high thread count):
The crash occurs inside:

HDC
TkWinGetDrawableDC(Display *display, Drawable d, TkWinDCState *state) {
    HDC dc;
    TkWinDrawable *twdPtr = (TkWinDrawable *)d;
    if (twdPtr->type == TWD_WINDOW) {
        ...
    }
}

Exception:
Unhandled exception thrown: read access violation.
**twdPtr** was nullptr.

Call stack:
>	tcl9tk91.dll!TkWinGetDrawableDC(_XDisplay * display, unsigned __int64 d, TkWinDCState * state) Line 154	C
 	tcl9tk91.dll!TkPutImage(unsigned long * colors, int ncolors, _XDisplay * display, unsigned __int64 d, XGCValues * gc, _XImage * image, int src_x, int src_y, int dest_x, int dest_y, unsigned int width, unsigned int height) Line 525	C
 	tcl9tk91.dll!XCreateBitmapFromData(_XDisplay * display, unsigned __int64 d, const char * data, unsigned int width, unsigned int height) Line 58	C
 	tcl9tk91.dll!GetBitmap(Tcl_Interp * interp, Tk_Window_ * tkwin, const char * string) Line 414	C
 	tcl9tk91.dll!Tk_GetBitmap(Tcl_Interp * interp, Tk_Window_ * tkwin, const char * string) Line 267	C
 	tcl9tk91.dll!TkButtonWorldChanged(void * instanceData) Line 1377	C
 	tcl9tk91.dll!ConfigureButton(Tcl_Interp * interp, TkButton * butPtr, __int64 objc, Tcl_Obj * const * objv) Line 1298	C
 	tcl9tk91.dll!ButtonCreate(void * dummy620, Tcl_Interp * interp, __int64 objc, Tcl_Obj * const * objv, int type) Line 739	C
 	tcl9tk91.dll!Tk_LabelObjCmd(void * clientData, Tcl_Interp * interp, int objc, Tcl_Obj * const * objv) Line 588	C
 	tcl91.dll!Dispatch(void * * data, Tcl_Interp * interp, int dummy4598) Line 4638	C
 	tcl91.dll!TclNRRunCallbacks(Tcl_Interp * interp, int result, NRE_callback * rootPtr) Line 4653	C
 	tcl91.dll!Tcl_EvalObjv(Tcl_Interp * interp, __int64 objc, Tcl_Obj * const * objv, int flags) Line 4392	C
 	tcl91.dll!TclEvalEx(Tcl_Interp * interp, const char * script, __int64 numBytes, int flags, int line, __int64 * clNextOuter, const char * outerScript) Line 5492	C
 	tcl91.dll!Tcl_EvalEx(Tcl_Interp * interp, const char * script, __int64 numBytes, int flags) Line 5144	C
 	tcl9thread302.dll!00007ffce0323feb()	Unknown
 	tcl91.dll!NewThreadProc(void * clientData) Line 2037	C
 	tcl91.dll!TclWinThreadStart(void * lpParameter) Line 181	C
 	ucrtbased.dll!00007ffce09f3010()	Unknown
 	kernel32.dll!00007ffd52bc7bd4()	Unknown
 	ntdll.dll!00007ffd5490cee1()	Unknown
  ...
  

Additional Observations:
- The issue only arises when `package require Tk` is used inside threads. I've only tested this on windows.
- At lower thread counts (e.g. 3), geometry corruption can be observed occasionally (`wm geom` shows invalid coordinates), and affected windows never become visible, though I could not reproduce this with  a "stock" tcl simple script. It only fails in a much larger program that can't be easily trimmed down for use in a ticket. The below script doesn't show that behavior, though it was the intention. Instead, it shows a crash. The 2 behaviors may not be from the same cause.
- Calling `wm geom` to reset the window's position has no effect; the window is not mapped.
- This behavior is new in Tk 9.1a0 and was never seen in 8.6 or 8.7 with the same application.

Here's a test script with 500 threads:

package require Thread
console show
for {set i 0} {$i < 500} {incr i} {
    set tid [thread::create {
        package require Tk
        toplevel .t
        label .t.l -text "Hello"
        pack .t.l
        wm geometry .t 200x100+100+[expr {int(rand()*400)}]
        update
        thread::wait
    }]
}
User Comments: anonymous added on 2025-07-24 09:03:25:
Ok, forget about the issue with bad geom's it appears that was caused by not having the magicsplat distro setup to use the very latest 9.1a0 dll's and was actually loading dll's from both 9.0 and 9.1. Once I got that fixed, so I could get all the debug symbols for the call stack, I also discovered that I couldn't get the corrupted geom anymore. Going back to the mismatched setup and the failure was reproduced quickly.

So, that's one less thing to worry about :) Sorry! 

But the crash is real.

anonymous added on 2025-07-24 08:16:01:
Ok, the crash is almost certainly the resource limit called GDI handles, which reached 10,000 and according to my research this is the limit in windows unless you increase it with a registry value.

Given that GDI handle limits are hit when creating many Tk windows in threads, it would be beneficial for Tk to monitor GDI resource usage and either enforce limits or gracefully return errors when resource exhaustion occurs, to prevent crashes and corrupted window states. This would improve stability in multi-threaded GUI applications.

However, I'm not convinced that this is the cause of the bogus geom value, since in that case I'm using far fewer resources. Unfortunately, I can't reliably force that error now.