Tk Source Code

Artifact [cd295ab0]
Login

Artifact cd295ab006eebe177489da060ef79093034de258:

Attachment "grid-dead-windows-fix.patch" to ticket [3021044f] added by a_kovalenko 2010-06-25 04:52:32.
diff -Naurd '--exclude=CVS' tk.orig/generic/tkGrid.c tk.new/generic/tkGrid.c
--- tk.orig/generic/tkGrid.c	2010-06-25 01:09:33.000000000 +0400
+++ tk.new/generic/tkGrid.c	2010-06-25 01:09:21.000000000 +0400
@@ -2382,8 +2382,8 @@
  *	tkwin.
  *
  * Side effects:
- *	A new grid structure may be created. If so, then a callback is set up
- *	to clean things up when the window is deleted.
+ *	A new grid structure may be allocated by TkGetWindowSpecificData.
+ *	If so, then it's deleted (Tcl_EventuallyFree'd) by Tk_DestroyWindow.
  *
  *----------------------------------------------------------------------
  */
@@ -2393,26 +2393,19 @@
     Tk_Window tkwin)		/* Token for window for which grid structure
 				 * is desired. */
 {
+    static char gridderDataKey[] = "data for tkGrid.c";
     register Gridder *gridPtr;
-    Tcl_HashEntry *hPtr;
     int isNew;
-    TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr;
-
-    if (!dispPtr->gridInit) {
-	Tcl_InitHashTable(&dispPtr->gridHashTable, TCL_ONE_WORD_KEYS);
-	dispPtr->gridInit = 1;
-    }
 
     /*
      * See if there's already grid for this window. If not, then create a new
      * one.
      */
-
-    hPtr = Tcl_CreateHashEntry(&dispPtr->gridHashTable, (char*) tkwin, &isNew);
+    gridPtr = TkGetWindowSpecificData(tkwin, &gridderDataKey, sizeof(*gridPtr),
+				      DestroyGrid, &isNew);
     if (!isNew) {
-	return Tcl_GetHashValue(hPtr);
+	return gridPtr;
     }
-    gridPtr = (Gridder *) ckalloc(sizeof(Gridder));
     gridPtr->tkwin = tkwin;
     gridPtr->masterPtr = NULL;
     gridPtr->masterDataPtr = NULL;
@@ -2437,7 +2430,6 @@
     gridPtr->sticky = 0;
     gridPtr->size = 0;
     gridPtr->masterDataPtr = NULL;
-    Tcl_SetHashValue(hPtr, gridPtr);
     Tk_CreateEventHandler(tkwin, StructureNotifyMask,
 	    GridStructureProc, gridPtr);
     return gridPtr;
@@ -2816,7 +2808,6 @@
     XEvent *eventPtr)		/* Describes what just happened. */
 {
     register Gridder *gridPtr = clientData;
-    TkDisplay *dispPtr = ((TkWindow *) gridPtr->tkwin)->dispPtr;
 
     if (eventPtr->type == ConfigureNotify) {
 	if ((gridPtr->slavePtr != NULL)
@@ -2845,13 +2836,10 @@
 	    nextPtr = gridPtr2->nextPtr;
 	    gridPtr2->nextPtr = NULL;
 	}
-	Tcl_DeleteHashEntry(Tcl_FindHashEntry(&dispPtr->gridHashTable,
-		(char *) gridPtr->tkwin));
 	if (gridPtr->flags & REQUESTED_RELAYOUT) {
 	    Tcl_CancelIdleCall(ArrangeGrid, gridPtr);
 	}
 	gridPtr->tkwin = NULL;
-	Tcl_EventuallyFree(gridPtr, DestroyGrid);
     } else if (eventPtr->type == MapNotify) {
 	if ((gridPtr->slavePtr != NULL)
 		&& !(gridPtr->flags & REQUESTED_RELAYOUT)) {
@@ -3250,31 +3238,47 @@
 	}
 
 	if (slavePtr->masterPtr == NULL) {
-	    Gridder *tempPtr = masterPtr->slavePtr;
+	    /* Null tkwin in gridder means the window is dead (from this
+	       module's point of view). As we use TkGetWindowSpecificData, all
+	       Gridders still remain valid, and we may handle dead windows as
+	       if they were alive-and-about-to-die (e.g. gridding a live
+	       window into a dead window means unlinking it from its previous
+	       master and unmapping it). About the only thing we must take
+	       care of is _not relinking dead slaves_ to any master and _not
+	       relinking dead masters_ to any slave: unlinking is done in
+	       DestroyNotify handler with no second chance, so we could get
+	       masterPtr and slavePtr pointing to invalidated Gridders if we
+	       didn't care.
+	     */
+	    if (slavePtr->tkwin && masterPtr->tkwin) {
+		Gridder *tempPtr = masterPtr->slavePtr;
 
-	    slavePtr->masterPtr = masterPtr;
-	    masterPtr->slavePtr = slavePtr;
-	    slavePtr->nextPtr = tempPtr;
+		slavePtr->masterPtr = masterPtr;
+		masterPtr->slavePtr = slavePtr;
+		slavePtr->nextPtr = tempPtr;
+	    }
 	}
-
-	/*
-	 * Make sure that the slave's parent is either the master or an
-	 * ancestor of the master, and that the master and slave aren't the
-	 * same.
+	/* Not doing anything with tkwin for dead Gridders is another thing
+	   required for avoiding dead window problems
 	 */
-
-	for (ancestor = masterPtr->tkwin; ; ancestor = Tk_Parent(ancestor)) {
-	    if (ancestor == parent) {
-		break;
-	    }
-	    if (Tk_TopWinHierarchy(ancestor)) {
-		Tcl_AppendResult(interp, "can't put ", Tcl_GetString(objv[j]),
-			" inside ", Tk_PathName(masterPtr->tkwin), NULL);
-		Unlink(slavePtr);
-		return TCL_ERROR;
+	if (masterPtr->tkwin) {
+	    /*
+	     * Make sure that the slave's parent is either the master or an
+	     * ancestor of the master, and that the master and slave aren't the
+	     * same.
+	     */
+	    for (ancestor = masterPtr->tkwin; ; ancestor = Tk_Parent(ancestor)) {
+		if (ancestor == parent) {
+		    break;
+		}
+		if (Tk_TopWinHierarchy(ancestor)) {
+		    Tcl_AppendResult(interp, "can't put ", Tcl_GetString(objv[j]),
+				     " inside ", Tk_PathName(masterPtr->tkwin), NULL);
+		    Unlink(slavePtr);
+		    return TCL_ERROR;
+		}
 	    }
 	}
-
 	/*
 	 * Try to make sure our master isn't managed by us.
 	 */
@@ -3286,19 +3290,24 @@
 	    Unlink(slavePtr);
 	    return TCL_ERROR;
      	}
-
-	Tk_ManageGeometry(slave, &gridMgrType, slavePtr);
+	if (slavePtr->tkwin) {
+	    Tk_ManageGeometry(slave, &gridMgrType, slavePtr);
+	}
 
 	if (!(masterPtr->flags & DONT_PROPAGATE)) {
-	    if (TkSetGeometryMaster(interp, masterPtr->tkwin, "grid")
+	    if (masterPtr->tkwin) {
+		if (TkSetGeometryMaster(interp, masterPtr->tkwin, "grid")
 		    != TCL_OK) {
+		    Tk_ManageGeometry(slave, NULL, NULL);
+		    Unlink(slavePtr);
+		    return TCL_ERROR;
+		}
+		masterPtr->flags |= ALLOCED_MASTER;
+	    } else {
+		/* master is dead */
 		Tk_ManageGeometry(slave, NULL, NULL);
-		Unlink(slavePtr);
-		return TCL_ERROR;
 	    }
-	    masterPtr->flags |= ALLOCED_MASTER;
 	}
-
 	/*
 	 * Assign default position information.
 	 */
@@ -3328,9 +3337,11 @@
 	if (masterPtr->abortPtr != NULL) {
 	    *masterPtr->abortPtr = 1;
 	}
-	if (!(masterPtr->flags & REQUESTED_RELAYOUT)) {
-	    masterPtr->flags |= REQUESTED_RELAYOUT;
-	    Tcl_DoWhenIdle(ArrangeGrid, masterPtr);
+	if (masterPtr->tkwin) {	/* don't request relayout for dead master */
+	    if (!(masterPtr->flags & REQUESTED_RELAYOUT)) {
+		masterPtr->flags |= REQUESTED_RELAYOUT;
+		Tcl_DoWhenIdle(ArrangeGrid, masterPtr);
+	    }
 	}
     }
 
diff -Naurd '--exclude=CVS' tk.orig/generic/tk.h tk.new/generic/tk.h
--- tk.orig/generic/tk.h	2010-06-25 01:10:03.000000000 +0400
+++ tk.new/generic/tk.h	2010-06-25 01:09:21.000000000 +0400
@@ -791,6 +791,7 @@
     int minReqWidth;
     int minReqHeight;
     char *dummy20;		/* geometryMaster */
+    char *dummy21;		/* windowSpecificDataPtr */
 } Tk_FakeWin;
 
 /*
diff -Naurd '--exclude=CVS' tk.orig/generic/tkInt.decls tk.new/generic/tkInt.decls
--- tk.orig/generic/tkInt.decls	2010-06-25 01:09:33.000000000 +0400
+++ tk.new/generic/tkInt.decls	2010-06-25 01:09:21.000000000 +0400
@@ -617,6 +617,11 @@
     CONST86 char *TkSmoothPrintProc(ClientData clientData, Tk_Window tkwin,
 	    char *widgRec, int offset, Tcl_FreeProc **freeProcPtr)
 }
+declare 181 generic {
+    ClientData TkGetWindowSpecificData(Tk_Window tkwin, ClientData key,
+				       int size, Tcl_FreeProc *freeProc,
+				       int *isNew)
+}
 
 ##############################################################################
 
diff -Naurd '--exclude=CVS' tk.orig/generic/tkIntDecls.h tk.new/generic/tkIntDecls.h
--- tk.orig/generic/tkIntDecls.h	2010-06-25 01:09:33.000000000 +0400
+++ tk.new/generic/tkIntDecls.h	2010-06-25 01:09:21.000000000 +0400
@@ -1112,6 +1112,13 @@
 				Tk_Window tkwin, char *widgRec, int offset,
 				Tcl_FreeProc **freeProcPtr);
 #endif
+#ifndef TkGetWindowSpecificData_TCL_DECLARED
+#define TkGetWindowSpecificData_TCL_DECLARED
+/* 181 */
+EXTERN ClientData	TkGetWindowSpecificData(Tk_Window tkwin,
+				ClientData key, int size,
+				Tcl_FreeProc *freeProc, int *isNew);
+#endif
 
 typedef struct TkIntStubs {
     int magic;
@@ -1397,6 +1404,7 @@
     CONST86 char * (*tkOrientPrintProc) (ClientData clientData, Tk_Window tkwin, char *widgRec, int offset, Tcl_FreeProc **freeProcPtr); /* 178 */
     int (*tkSmoothParseProc) (ClientData clientData, Tcl_Interp *interp, Tk_Window tkwin, const char *value, char *widgRec, int offset); /* 179 */
     CONST86 char * (*tkSmoothPrintProc) (ClientData clientData, Tk_Window tkwin, char *widgRec, int offset, Tcl_FreeProc **freeProcPtr); /* 180 */
+    ClientData (*tkGetWindowSpecificData) (Tk_Window tkwin, ClientData key, int size, Tcl_FreeProc *freeProc, int *isNew); /* 181 */
 } TkIntStubs;
 
 #if defined(USE_TK_STUBS) && !defined(USE_TK_STUB_PROCS)
@@ -2158,6 +2166,10 @@
 #define TkSmoothPrintProc \
 	(tkIntStubsPtr->tkSmoothPrintProc) /* 180 */
 #endif
+#ifndef TkGetWindowSpecificData
+#define TkGetWindowSpecificData \
+	(tkIntStubsPtr->tkGetWindowSpecificData) /* 181 */
+#endif
 
 #endif /* defined(USE_TK_STUBS) && !defined(USE_TK_STUB_PROCS) */
 
diff -Naurd '--exclude=CVS' tk.orig/generic/tkInt.h tk.new/generic/tkInt.h
--- tk.orig/generic/tkInt.h	2010-06-25 01:09:33.000000000 +0400
+++ tk.new/generic/tkInt.h	2010-06-25 01:09:21.000000000 +0400
@@ -818,6 +818,10 @@
     int minReqWidth;		/* Minimum requested width. */
     int minReqHeight;		/* Minimum requested height. */
     char *geometryMaster;
+
+    /* Linked list of data pointers related to a particular tkwin and various
+     * subsystems */
+    struct TkWindowSpecificData *windowSpecificDataPtr;
 } TkWindow;
 
 /*
diff -Naurd '--exclude=CVS' tk.orig/generic/tkStubInit.c tk.new/generic/tkStubInit.c
--- tk.orig/generic/tkStubInit.c	2010-06-25 01:09:33.000000000 +0400
+++ tk.new/generic/tkStubInit.c	2010-06-25 01:09:21.000000000 +0400
@@ -334,6 +334,7 @@
     TkOrientPrintProc, /* 178 */
     TkSmoothParseProc, /* 179 */
     TkSmoothPrintProc, /* 180 */
+    TkGetWindowSpecificData, /* 181 */
 };
 
 static const TkIntPlatStubs tkIntPlatStubs = {
diff -Naurd '--exclude=CVS' tk.orig/generic/tkWindow.c tk.new/generic/tkWindow.c
--- tk.orig/generic/tkWindow.c	2010-06-25 01:09:33.000000000 +0400
+++ tk.new/generic/tkWindow.c	2010-06-25 01:09:21.000000000 +0400
@@ -19,6 +19,7 @@
 #if !( defined(__WIN32__) || defined(MAC_OSX_TK))
 #include "tkUnixInt.h"
 #endif
+#include <string.h>
 
 /*
  * Type used to keep track of Window objects that were only partially
@@ -37,6 +38,13 @@
     struct TkHalfdeadWindow *nextPtr;
 } TkHalfdeadWindow;
 
+typedef struct TkWindowSpecificData {
+    ClientData key;
+    ClientData data;
+    Tcl_FreeProc* freeProc;
+    struct TkWindowSpecificData* nextPtr;
+} TkWindowSpecificData;
+
 typedef struct ThreadSpecificData {
     int numMainWindows;		/* Count of numver of main windows currently
 				 * open in this thread. */
@@ -710,6 +718,7 @@
     winPtr->minReqWidth = 0;
     winPtr->minReqHeight = 0;
     winPtr->geometryMaster = NULL;
+    winPtr->windowSpecificDataPtr = NULL;
 
     return winPtr;
 }
@@ -1237,6 +1246,72 @@
 /*
  *--------------------------------------------------------------
  *
+ * TkGetWindowSpecificData --
+ *
+ *      Retrive a block of tkwin-related data identified by the data key,
+ *      allocating it if needed.
+ *
+ * Resullts:
+ *      Pointer to a data block.
+ *
+ * Side effects:
+ *      If isNew pointer is non-NULL, boolean value indicating whether the new
+ *      block was allocated is stored to a place pointed by it. 
+ */
+ClientData
+TkGetWindowSpecificData(Tk_Window tkwin, ClientData key, int size, 
+			Tcl_FreeProc* freeProc, int *isNew)
+{
+    TkWindow *winPtr = (TkWindow*)tkwin;
+    TkWindowSpecificData *dataPtr = winPtr->windowSpecificDataPtr;
+    while (dataPtr) {
+	if (dataPtr->key == key) {
+	    if (isNew)
+		*isNew = 0;
+	    return dataPtr->data;
+	}
+	dataPtr = dataPtr->nextPtr;
+    }
+    /* no data block was found, let's allocate one */
+    dataPtr = (TkWindowSpecificData*)ckalloc(sizeof(*dataPtr));
+    dataPtr->key = key;
+    dataPtr->freeProc = freeProc;
+    dataPtr->data = ckalloc(size);
+    memset(dataPtr->data,0,size);
+    dataPtr->nextPtr = winPtr->windowSpecificDataPtr;
+    winPtr->windowSpecificDataPtr = dataPtr;
+    if (isNew)
+	*isNew = 1;
+    return dataPtr->data;
+}
+
+/*
+ *--------------------------------------------------------------
+ *
+ * FreeWindowSpecificData --
+ *
+ *      Free any window-specific data blocks associated with tkwin,
+ *      using their freeProcs and Tcl_EventuallyFree.
+ *
+ * Resullts:
+ *      None.
+ */
+static void FreeWindowSpecificData(Tk_Window tkwin)
+{
+    TkWindow* winPtr = (TkWindow*)tkwin;
+    TkWindowSpecificData *dataPtr = winPtr->windowSpecificDataPtr, *nextPtr;
+    while (dataPtr) {
+	nextPtr = dataPtr->nextPtr;
+	Tcl_EventuallyFree(dataPtr->data, dataPtr->freeProc);
+	ckfree((char*)dataPtr);
+	dataPtr = nextPtr;
+    }
+}
+
+
+/*
+ *--------------------------------------------------------------
+ *
  * Tk_DestroyWindow --
  *
  *	Destroy an existing window. After this call, the caller should never
@@ -1613,6 +1688,7 @@
 #endif
 	}
     }
+    FreeWindowSpecificData(tkwin);
     Tcl_EventuallyFree(winPtr, TCL_DYNAMIC);
 }