Tk Source Code

Artifact [666bc5ff]
Login

Artifact 666bc5fff0780d62cc30bd76a2c060e6f2a18cddb4605e3e9f12432e7e6199d4:

Attachment "tkMenu-core-8-6-branch.patch" to ticket [a680524f] added by anonymous 2026-11-18 16:14:04. (unpublished)
--- a/generic/tkMenu.c
+++ b/generic/tkMenu.c
@@ -3755,13 +3755,23 @@
     int last)			/* The zero-based last entry. */
 {
     TkMenu *menuListPtr;
+    TkMenuEntry **deletedEntries;
     int numDeleted, i, j;
 
     numDeleted = last + 1 - first;
+    deletedEntries = (TkMenuEntry **)ckalloc(numDeleted * sizeof(TkMenuEntry *));
     for (menuListPtr = menuPtr->masterMenuPtr; menuListPtr != NULL;
 	    menuListPtr = menuListPtr->nextInstancePtr) {
-	for (i = last; i >= first; i--) {
-	    Tcl_EventuallyFree(menuListPtr->entries[i], DestroyMenuEntry);
+	/*
+	 * Remove the entries from the menu before freeing them. Freeing a
+	 * cascade entry of a clone destroys the clone of its menu, which can
+	 * service events (on Windows DestroyWindow calls TkWinChildProc, which
+	 * calls Tcl_ServiceAll), e.g. a pending ReconfigureWindowsMenu of this
+	 * menu, which must not find freed entries in menuListPtr->entries.
+	 */
+
+	for (i = first; i <= last; i++) {
+	    deletedEntries[i - first] = menuListPtr->entries[i];
 	}
 	for (i = last + 1; i < menuListPtr->numEntries; i++) {
 	    j = i - numDeleted;
@@ -3780,7 +3790,11 @@
 	    menuListPtr->active -= numDeleted;
 	}
 	TkEventuallyRecomputeMenu(menuListPtr);
+	for (i = numDeleted - 1; i >= 0; i--) {
+	    Tcl_EventuallyFree(deletedEntries[i], DestroyMenuEntry);
+	}
     }
+    ckfree(deletedEntries);
 }
 
 /*