Tk Source Code

Artifact [02a73a6a]
Login

Artifact 02a73a6a3863843121750c2a30c8b39fd62d5c8d55fbfd9542fc61a79420375c:

Attachment "tkMenu-main.patch" to ticket [a680524f] added by anonymous 2026-11-18 16:14:04. (unpublished)
--- a/generic/tkMenu.c
+++ b/generic/tkMenu.c
@@ -3939,13 +3939,23 @@
     int last)			/* The zero-based last entry. */
 {
     TkMenu *menuListPtr;
+    TkMenuEntry **deletedEntries;
     Tcl_Size numDeleted, i, j;
 
     numDeleted = last + 1 - first;
+    deletedEntries = (TkMenuEntry **)Tcl_Alloc(numDeleted * sizeof(TkMenuEntry *));
     for (menuListPtr = menuPtr->mainMenuPtr; 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;
@@ -3964,7 +3974,11 @@
 	    menuListPtr->active -= numDeleted;
 	}
 	TkEventuallyRecomputeMenu(menuListPtr);
+	for (i = numDeleted - 1; i >= 0; i--) {
+	    Tcl_EventuallyFree(deletedEntries[i], DestroyMenuEntry);
+	}
     }
+    Tcl_Free(deletedEntries);
 }
 
 /*