Tk Source Code

View Ticket
Login
2026-11-10
22:10 • Ticket [aeb2ed51] Aqua: Unretained NSWindow reference passed to showRootWindow() idle task status still Closed with 6 other changes artifact: 8fca93eb user: nflyernz
2026-10-29
20:49
Add TkpReadPixelSRGB and backport fix for [aeb2ed51ab]. check-in: 8c5a7dad user: culler tags: core-9-0-branch
20:02 • Closed ticket [aeb2ed51]: Aqua: Unretained NSWindow reference passed to showRootWindow() idle task plus 8 other changes artifact: a05de08a user: marc_culler
19:46
Fix [aeb2ed51ab]: Aqua: Unretained NSWindow reference passed to showRootWindow() idle task check-in: 7a4499e2 user: culler tags: trunk, main
2026-10-24
10:39 • Ticket [aeb2ed51] Aqua: Unretained NSWindow reference passed to showRootWindow() idle task status still Open with 4 other changes artifact: a09821cc user: serhiy.storchaka
10:39 • Ticket [8e32cb7e] Aqua: window-2.12 crash status still Open with 4 other changes artifact: 066100ef user: serhiy.storchaka
10:33
Fix [aeb2ed51ab] and [8e32cb7ece]: on macOS, destroying "." before the root window was first shown (e.g. [after 0 {destroy .}]) crashed in the showRootWindow() idle handler, which used the possibly deallocated NSWindow and the NULL Tk window. Retain the NSWindow until the handler runs, and do nothing there if its Tk window is gone. Also fix a background error on Windows in the console, whose idle script for erasing the "% " prompt ran after the application was destroyed. Test window-2.13. check-in: 996b73f3 user: serhiy.storchaka tags: macos-root-destroyed-early
2025-11-30
03:09 • Ticket [8e32cb7e] Aqua: window-2.12 crash status still Open with 4 other changes artifact: f9053691 user: chrstphrchvz
2025-11-29
03:55 • Ticket [aeb2ed51] Aqua: Unretained NSWindow reference passed to showRootWindow() idle task status still Open with 4 other changes artifact: 2731a40a user: chrstphrchvz
01:37 • New ticket [aeb2ed51]. artifact: f6ffb75b user: chrstphrchvz

Ticket UUID: aeb2ed51abc45f7b544c7695875bee6d013a580a
Title: Aqua: Unretained NSWindow reference passed to showRootWindow() idle task
Type: Bug Version: 9.0.5, 9.1.0
Submitter: chrstphrchvz Created on: 2025-11-29 01:37:46
Subsystem: 66. Aqua Window Operations Assigned To: chrstphrchvz
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2026-11-10 22:10:03
Resolution: Fixed Closed By: nflyernz
    Closed on: 2026-11-10 22:10:03
Description:

Just a potential issue I notice from code review, not an observed crash. In TkpInit(), there is this call:

Tcl_DoWhenIdle(showRootWindow, window);

It seems possible that the NSWindow window could get deallocated before showRootWindow() is finally called. I would think window should be retained first and then released during showRootWindow(). Or maybe Tcl_CancelIdleCall(showRootWindow, …) should be called from somewhere instead, although showRootWindow() is currently static.

User Comments: nflyernz added on 2026-11-10 22:10:03:
 ### Independent macOS ARM64 verification of the `showRootWindow()` fix

I encountered a native Tkinter crash while investigating Python 3.15.2 GUI tests in ArduPilot MethodicConfigurator.

The problem was reduced to this standalone reproducer, requiring no application code or third-party modules:

```python
import tkinter as tk

first = tk.Tk()
first.destroy()

second = tk.Tk()
second.update_idletasks()
second.destroy()
```

On macOS 26.7.2 ARM64, this consistently produced a native segmentation fault in the diagnostic runs.

The failure reproduced with:
- python.org/setup-python CPython 3.15.2, Tk 9.0.5
- uv-managed CPython 3.15.2, Tk 9.0.4
- python.org/setup-python CPython 3.14.10

The uv-managed build identified `showRootWindow+0x38` in the native crash stack.

I then performed a controlled comparison using two isolated Tk builds from the same pre-fix revision, applying only the fix for ticket `aeb2ed51ab` to the second build.

**Results:**
- W1 — unpatched: segmentation fault, exit 139
- W2 — patched: completed successfully, exit 0

Both trials used identical Python 3.15.2, `_tkinter`, Tcl 9.0.4 and macOS environment. Dynamic-library loading was recorded and verified to confirm that each process used the intended Tk binary.

The patch changes were limited to `macosx/tkMacOSXInit.c`.

Evidence and native traces:
https://github.com/nflyernz/MethodicConfigurator/actions/runs/42908025736

Previous reduction and distribution comparisons:
https://github.com/nflyernz/MethodicConfigurator/actions/runs/42901204212

https://github.com/nflyernz/MethodicConfigurator/actions/runs/42902938334

This provides independent confirmation that the existing upstream fix prevents this particular `showRootWindow()` crash on macOS ARM64.

It does not establish that other Tkinter lifecycle failures, geometry stalls or menu-validation crashes are resolved.

Thank you for the fix.

marc_culler (claiming to be Marc Culler) added on 2026-10-29 20:02:01:
The macos-root-destroyed-early branch stops this crash.  I have merged
that branch into trunk and will close this ticket.

serhiy.storchaka added on 2026-10-24 10:39:54:

Fixed on branch macos-root-destroyed-early [996b73f3a3], as suggested: window is retained before Tcl_DoWhenIdle() and released in showRootWindow(). The same commit fixes the crash in [8e32cb7ece], which happens once this one is avoided. New test window-2.13 runs the script from there.

Tested on Linux and Windows. I have no Mac, so could someone test it on macOS?


chrstphrchvz added on 2025-11-29 03:55:37:

The crash predicted by this ticket can be triggered by this script (adapted from test window-2.12):

package require Tk
after 1000 {set forever 1}
after 0 {destroy .}
puts "waiting"
vwait forever
puts "done waiting"

Output with NSZombieEnabled=YES:

waiting
2025-11-28 21:54:06.698 tclsh9.0[21191:236550] *** -[TKWindow respondsToSelector:]: message sent to deallocated instance 0x615000041a80
zsh: illegal hardware instruction