Tk Source Code

View Ticket
Login
Ticket UUID: c77b426dc97105c5b1b9d6b0e94ec0728c560a66
Title: panic with [menu] + [wm forget]
Type: Bug Version: 8.6-9
Submitter: emiliano Created on: 2026-01-13 18:51:09
Subsystem: 12. Unix Menus Assigned To: nobody
Priority: 5 Medium Severity: Critical
Status: Closed Last Modified: 2026-01-25 17:55:14
Resolution: Fixed Closed By: oehhar
    Closed on: 2026-01-25 17:55:14
Description:
Combining [wm forget] with [menu] fires a Tcl_Panic in Tk_MoveToplevelWindow

$ wish9.1 
% menu .m
% wm forget .m
% tk_popup .m 100 100
Tk_MoveToplevelWindow called with non-toplevel window
Illegal instruction (core dumped)

Confirmed to crash in X11 on several Tk versions, from 8.5 to trunk
User Comments: oehhar added on 2026-01-25 17:55:14:
Changelogs committed

oehhar added on 2026-01-23 20:51:46:
Reminder to self: Changelog for 8.6 and 9.0 pending.

oehhar added on 2026-01-23 20:50:11:
Thanks! At least, one of the many branches with Christian or Emiliano for tk committed.
Branch and ticket closed!

emiliano added on 2026-01-23 20:25:09:
Commited to trunk, core-9-0-branch and core-8-6-branch.

Thanks!

oehhar added on 2026-01-23 12:23:04:
I would be in favor to merge this.
Emiliano, if you are also in favor, you can merge to main and eventually other branches.

Thanks for all,
Harald

emiliano added on 2026-01-22 21:12:48:

@kevin_walzer: thanks for the confirmation. In the chat, the user rhobart also confirmed that running this snippet

package require Tk
menu .m
.m add command -label foo
wm forget .m
.m post 100 100

doesn't crash on MacOS with both 8.6.16 and 9.0.1.

I added a constraint to the new test so that it only runs on x11. Please test.

Thanks


kevin_walzer added on 2026-01-22 20:20:15:
Menus on macOS are native.

emiliano added on 2026-01-22 19:21:45:

Working interactively on Windows (old Win8, the only I have available), with the following session

% menu .m; .m add command -label foo
e001
% after 1000 {wm forget .m; puts ouch!; puts [winfo ismapped .m]}; .m post 100 100
ouch!
0

shows that

  • [wm forget] has no effect on menus. .m remains posted after [wm forget]
  • [.m post 100 100] doesn't return until the menu is dismissed. It does, however, process events while posted.
  • [winfo ismapped] doesn't work with menus either.

AIUI, menus on windows are native while on X11 they are managed by Tk. This could explain the difference. I don't know how they are implemented on MacOS, but my guess is that they are also native.

This looks like is an x11 exclusive bug to me. In such case, the test should have the x11 constraint added (and the added check is redundant, at least on windows).


oehhar added on 2026-01-22 18:25:35:

Thanks, Emiliano, for the patch. It is now in:

  • Checkin [a7ce5c65]
  • Branch [c77b426d-menu-wm-forget-panic]

I have run the test suite on MS-Win. Unfortunately, the new test case fails:

==== wm-forget-2.1 bug [c77b426dc9] - panic with [menu]+[wm forget] FAILED
==== Contents of test case:

    menu .m
    .m post 100 100
    update
    lappend res [winfo ismapped .m]
    wm forget .m
    update
    lappend res [winfo ismapped .m]

---- Result was:
0 0
---- Result should have been (exact matching):
1 1
==== wm-forget-2.1 FAILED

The main question is, if it crashes. This is not the case.

Remark, that there is no crash with the original lines neither.

So, adding a label at least shows something:

.m add cascade -label test
but the command ".m post 100 100" blocks until the focus is changed, which makes the menu dissappear.

With tcl 9.0, I can not reproduce the issue even with a "wm forget .m" passed.

Take care, Harald


chw added on 2026-01-22 07:35:49:
@emiliano: indeed, a much cleaner and better solution.

emiliano added on 2026-01-21 23:58:47:
@chw: Sorry, but I don't think this is the right place to fix this bug.

When [wm manage]/[wm forget] was introduced, a new flag and associated macro was added to the public API: TK_WM_MANAGEABLE and Tk_IsManageable(). The bug is that this property was not tested in the "forget" case, only in the "manage". In the former, the code just checks for the window being a toplevel. Adding the required check turns [wm forget] for menus a noop, in line with the rest of the widgets without the flag.

The attached patch add this check to all platforms and provides a new test case.

chw added on 2026-01-15 19:30:46:
Maybe this to proof-of-concept fix is helpful in this regard:

  https://androwish.org/home/info/26d895a8dcb86c28

Attachments: