| User Comments: |
serhiy.storchaka added on 2026-09-25 08:19:19:
Fixed in 8.6.13 by [d638e7ca36], which removed the autorelease pool reset from TkWmDeadWindow(), as chrstphrchvz found below. The crash happens only with Tk 8.6.12 and older, e.g. the Tk bundled with older Python installers. Closing.
chrstphrchvz added on 2023-07-03 02:11:40:
The script for this ticket does not appear to be affected by the TKContentView KVO bug [ef5d3e29a4]; it crashes when I apply the fix proposed in that ticket while also reverting [d638e7ca36]. This prematurely released autorelease pool bug is separate from the TKContentView KVO bug, even though the former makes it easier to trigger the latter.
For reference, here is a stack trace (for Tcl equivalent script) when _resetAutoreleasePool is about to be called:
* frame #0: 0x00000001092f6603 libtcl9tk8.7.dylib`TkWmDeadWindow(winPtr=0x0000000100116390) at tkMacOSXWm.c:1298:6
frame #1: 0x0000000108c42e2e libtcl9tk8.7.dylib`Tk_DestroyWindow(tkwin=0x0000000100116390) at tkWindow.c:1530:2
frame #2: 0x0000000108ad8dc3 libtcl9tk8.7.dylib`Tk_DestroyObjCmd(clientData=0x0000000100116390, interp=0x0000000103809800, objc=2, objv=0x0000600003706020) at tkCmds.c:501:2
frame #3: 0x00000001014de754 libtcl9.0.dylib`Dispatch(data=0x0000600000cf6048, interp=0x0000000103809800, dummy4591=0) at tclBasic.c:4629:12
frame #4: 0x00000001014c85b7 libtcl9.0.dylib`TclNRRunCallbacks(interp=0x0000000103809800, result=0, rootPtr=0x0000000000000000) at tclBasic.c:4645:11
frame #5: 0x00000001014d6cd6 libtcl9.0.dylib`TclEvalObjEx(interp=0x0000000103809800, objPtr=0x0000600000ceb630, flags=131072, invoker=0x0000000000000000, word=0) at tclBasic.c:6104:12
frame #6: 0x00000001014d6b9a libtcl9.0.dylib`Tcl_EvalObjEx(interp=0x0000000103809800, objPtr=0x0000600000ceb630, flags=131072) at tclBasic.c:6085:12
frame #7: 0x0000000101eb8938 libtcl9.0.dylib`AfterProc(clientData=0x0000600000cebb70) at tclTimer.c:1182:14
frame #8: 0x0000000101eb382b libtcl9.0.dylib`TclServiceIdle at tclTimer.c:751:2
frame #9: 0x0000000101db341e libtcl9.0.dylib`Tcl_DoOneEvent(flags=34) at tclNotify.c:1020:10
frame #10: 0x00000001092e4820 libtcl9tk8.7.dylib`-[TKContentView(self=0x00000001012219d0, _cmd="generateExposeEvents:", rect=(origin = (x = 0, y = 0), size = (width = 200, height = 200))) generateExposeEvents:] at tkMacOSXWindowEvent.c:1198:9
frame #11: 0x00000001092e3c41 libtcl9tk8.7.dylib`-[TKContentView(self=0x00000001012219d0, _cmd="drawRect:", rect=(origin = (x = 0, y = 0), size = (width = 200, height = 200))) drawRect:] at tkMacOSXWindowEvent.c:1069:5
frame #12: 0x00007ff80cb98a00 AppKit`_NSViewDrawRect + 121
frame #13: 0x00007ff80d3785ea AppKit`-[NSView _recursive:displayRectIgnoringOpacity:inContext:stopAtLayerBackedViews:] + 1810
frame #14: 0x00007ff80cb9812f AppKit`-[NSView(NSLayerKitGlue) _drawViewBackingLayer:inContext:drawingHandler:] + 753
frame #15: 0x00007ff81127b180 QuartzCore`CABackingStoreUpdate_ + 246
frame #16: 0x00007ff8112dd891 QuartzCore`invocation function for block in CA::Layer::display_() + 53
frame #17: 0x00007ff81127a14b QuartzCore`-[CALayer _display] + 2289
frame #18: 0x00007ff8112790c0 QuartzCore`CA::Layer::display_if_needed(CA::Transaction*) + 856
frame #19: 0x00007ff81140018b QuartzCore`CA::Context::commit_transaction(CA::Transaction*, double, double*) + 645
frame #20: 0x00007ff811259ff3 QuartzCore`CA::Transaction::commit() + 713
frame #21: 0x00007ff80cba8fcf AppKit`__62+[CATransaction(NSCATransaction) NS_setFlushesWithDisplayLink]_block_invoke + 285
frame #22: 0x00007ff80d3bbe90 AppKit`___NSRunLoopObserverCreateWithHandler_block_invoke + 41
frame #23: 0x00007ff8099b7444 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
frame #24: 0x00007ff8099b736b CoreFoundation`__CFRunLoopDoObservers + 482
frame #25: 0x00007ff8099b68f6 CoreFoundation`__CFRunLoopRun + 859
frame #26: 0x00007ff8099b5f31 CoreFoundation`CFRunLoopRunSpecific + 560
frame #27: 0x00007ff813431dad HIToolbox`RunCurrentEventLoopInMode + 292
frame #28: 0x00007ff813431a19 HIToolbox`ReceiveNextEventCommon + 236
frame #29: 0x00007ff813431918 HIToolbox`_BlockUntilNextEventMatchingListInModeWithFilter + 64
frame #30: 0x00007ff80ca4a5d0 AppKit`_DPSNextEvent + 858
frame #31: 0x00007ff80ca4947a AppKit`-[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1214
frame #32: 0x00000001092bc4a2 libtcl9tk8.7.dylib`TkMacOSXEventsSetupProc(dummy=0x0000000000000000, flags=-3) at tkMacOSXNotify.c:481:10
frame #33: 0x0000000101db2e45 libtcl9.0.dylib`Tcl_DoOneEvent(flags=-3) at tclNotify.c:970:3
frame #34: 0x0000000108b2d688 libtcl9tk8.7.dylib`Tk_MainLoop at tkEvent.c:2124:2
frame #35: 0x0000000101d858f9 libtcl9.0.dylib`Tcl_MainEx(argc=-1, argv=0x00007ff7bfeff820, appInitProc=(tclsh9.0`Tcl_AppInit at tclAppInit.c:120), interp=0x0000000103809800) at tclMain.c:594:2
frame #36: 0x0000000100003a38 tclsh9.0`main(argc=2, argv=0x00007ff7bfeff820) at tclAppInit.c:94:5
frame #37: 0x00007ff80958241f dyld`start + 1903
Stack trace after crash:
* frame #0: 0x00007ff8098a7a42 libsystem_kernel.dylib`__abort_with_payload + 10
frame #1: 0x00007ff8098c586e libsystem_kernel.dylib`abort_with_payload_wrapper_internal + 82
frame #2: 0x00007ff8098c581c libsystem_kernel.dylib`abort_with_reason + 19
frame #3: 0x00007ff809568691 libobjc.A.dylib`_objc_fatalv(unsigned long long, unsigned long long, char const*, __va_list_tag*) + 114
frame #4: 0x00007ff80956861f libobjc.A.dylib`_objc_fatal(char const*, ...) + 138
frame #5: 0x00007ff809569331 libobjc.A.dylib`AutoreleasePoolPage::badPop(void*) + 225
frame #6: 0x00007ff80d378a14 AppKit`-[NSView _recursive:displayRectIgnoringOpacity:inContext:stopAtLayerBackedViews:] + 2876
frame #7: 0x00007ff80cb9812f AppKit`-[NSView(NSLayerKitGlue) _drawViewBackingLayer:inContext:drawingHandler:] + 753
frame #8: 0x00007ff81127b180 QuartzCore`CABackingStoreUpdate_ + 246
frame #9: 0x00007ff8112dd891 QuartzCore`invocation function for block in CA::Layer::display_() + 53
frame #10: 0x00007ff81127a14b QuartzCore`-[CALayer _display] + 2289
frame #11: 0x00007ff8112790c0 QuartzCore`CA::Layer::display_if_needed(CA::Transaction*) + 856
frame #12: 0x00007ff81140018b QuartzCore`CA::Context::commit_transaction(CA::Transaction*, double, double*) + 645
frame #13: 0x00007ff811259ff3 QuartzCore`CA::Transaction::commit() + 713
frame #14: 0x00007ff80cba8fcf AppKit`__62+[CATransaction(NSCATransaction) NS_setFlushesWithDisplayLink]_block_invoke + 285
frame #15: 0x00007ff80d3bbe90 AppKit`___NSRunLoopObserverCreateWithHandler_block_invoke + 41
frame #16: 0x00007ff8099b7444 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
frame #17: 0x00007ff8099b736b CoreFoundation`__CFRunLoopDoObservers + 482
frame #18: 0x00007ff8099b68f6 CoreFoundation`__CFRunLoopRun + 859
frame #19: 0x00007ff8099b5f31 CoreFoundation`CFRunLoopRunSpecific + 560
frame #20: 0x00007ff813431dad HIToolbox`RunCurrentEventLoopInMode + 292
frame #21: 0x00007ff813431a19 HIToolbox`ReceiveNextEventCommon + 236
frame #22: 0x00007ff813431918 HIToolbox`_BlockUntilNextEventMatchingListInModeWithFilter + 64
frame #23: 0x00007ff80ca4a5d0 AppKit`_DPSNextEvent + 858
frame #24: 0x00007ff80ca4947a AppKit`-[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1214
frame #25: 0x00000001092bc4a2 libtcl9tk8.7.dylib`TkMacOSXEventsSetupProc(dummy=0x0000000000000000, flags=-3) at tkMacOSXNotify.c:481:10
frame #26: 0x0000000101db2e45 libtcl9.0.dylib`Tcl_DoOneEvent(flags=-3) at tclNotify.c:970:3
frame #27: 0x0000000108b2d688 libtcl9tk8.7.dylib`Tk_MainLoop at tkEvent.c:2124:2
frame #28: 0x0000000101d858f9 libtcl9.0.dylib`Tcl_MainEx(argc=-1, argv=0x00007ff7bfeff820, appInitProc=(tclsh9.0`Tcl_AppInit at tclAppInit.c:120), interp=0x0000000103809800) at tclMain.c:594:2
frame #29: 0x0000000100003a38 tclsh9.0`main(argc=2, argv=0x00007ff7bfeff820) at tclAppInit.c:94:5
frame #30: 0x00007ff80958241f dyld`start + 1903
Error message on macOS 13.4.1:
objc[12453]: Invalid or prematurely-freed autorelease pool 0x7fcf0500b930. Set a breakpoint on objc_autoreleasePoolInvalid to debug.
objc[12453]: Invalid autorelease pools are a fatal error
-[NSView _recursive:displayRectIgnoringOpacity:inContext:stopAtLayerBackedViews:] creates a temporary autorelease pool and calls drawRect:. (I am ignoring for now whether Tk Aqua expects drawRect: to be called within TkMacOSXEventsSetupProc().) The idle event destroying the toplevel is handled during generateExposeEvents: while in drawRect:. _resetAutoreleasePool is called, and it drains the main pool since nothing in the call stack has locked it, which releases any nested pools including the one created by _recursive:displayRectIgnoringOpacity:inContext:stopAtLayerBackedViews:. The crash occurs when that function then tries to release its temporary pool, since it was prematurely freed by _resetAutoreleasePool.
There are other private functions which use temporary autorelease pools and would be affected by the same issue if _resetAutoreleasePool is ever called within them while the main pool is unlocked. These include __NSThreadPerformPerform and -[NSApplication _nextEventMatchingEventMask:untilDate:inMode:dequeue:], as seen in the stack trace for [fc5073a451]: although Tcl events currently stop during native modal dialogs, an exception is when the file type filter menu is posted, since that causes TKBackgroundLoop to run, which in turn can handle an event to destroy a toplevel and call _resetAutoreleasePool.
Although [d638e7ca36] may appear to avoid this issue, I would not say this problem is completely eliminated as long as _resetAutoreleasePool could potentially be called while in a function that creates a nested pool and while the main pool is unlocked. So far the only places the main pool is locked from are TkMacOSXEventsCheckProc() and setFrameSize:, which is not enough to prevent the crash in this ticket nor the one in [fc5073a451] (which involves showOpenSavePanel() rather than TkMacOSXEventsSetupProc() and drawRect:).
chrstphrchvz added on 2023-04-05 15:24:00:
This seems to be fixed in the bug-ef5d3e29a4-redux branch as well.
Does it make a difference though? I have only managed to get the reporter’s script to crash on macOS 12.6.4 Intel with Python’s bundled Tk Aqua 8.6.12 (although the error is instead “Invalid or prematurely-freed autorelease pool”); the crash goes away once I patch libtk8.6.dylib to replace [NSApp _resetAutoreleaseCall] in TkWmDeadWindow() with a NOP instruction. Nor would the crash occur with Tk Aqua 8.6.13 from MacPorts. The Tcl equivalent package require Tk; after idle {destroy .} also would not crash for 8.6.13 or current core-8-6-branch.
marc_culler (claiming to be Marc Culler) added on 2023-04-05 02:23:04:
This seems to be fixed in the bug-ef5d3e29a4-redux branch as well.
chrstphrchvz added on 2023-04-04 19:07:24:
Sorry, the mention of the autorelease pool page … corrupted error was in earlier comments of [fc5073a451].
chrstphrchvz added on 2023-04-04 18:59:32:
Can the reporter please confirm the patchlevel (output of tkinter.Tk().info_patchlevel())?
chrstphrchvz added on 2023-04-04 18:55:30:
Both the test case and the crash error here resemble what I mentioned in my comment dated 2022-04-10 19:24:10 on [fc5073a451]. The crash seems to be resolved in Tcl/Tk 8.6.13 by this change: [d638e7ca3616]. Currently the official Python distributions (if that is what the reporter is using) have up to Tcl/Tk 8.6.12.
|