Tk Source Code

Artifact [c4282821]
Login

Artifact c4282821ea7fa395510fa7faea154c526c56f879:

Attachment "xim.patch4" to ticket [570902ff] added by mdejong 2002-06-19 09:18:35.
2002-06-18  Mo DeJong  <[email protected]>

	* generic/tkBind.c (TkBindDeadWindow):
	Handle case where Tk_DestroyWindow is invoked
	on clipboard and send windows.
	* generic/tkClipboard.c (TkClipCleanup):
	Invoke Tk_DestroyWindow to cleanup the
	dispPtr->clipWindow. Call Tcl_Preserve
	and Tcl_Release on the window to avoid an
	invalid memory ref on shutdown.
	* generic/tkEvent.c (Tk_HandleEvent):
	Panic if XCreateIC is invoked twice for
	the same window. This should never happen,
	the check were just added to make sure it
	does not since this could lead to crashes
	in XCloseIM.
	* generic/tkFocus.c (TkFocusDeadWindow):
	Handle case where Tk_DestroyWindow is invoked
	on clipboard and send windows.
	* generic/tkOption.c (TkOptionDeadWindow): Ditto.
	* generic/tkWindow.c (TkCloseDisplay): Move
	deletion of dispPtr->winTable after TkpCloseDisplay
	call since Tk_DestroyWindow uses it and could
	be called by TkpCloseDisplay for clipboard/send windows.
	Also invoke ckfree for the dispPtr instead of
	doing it in TkpCloseDisplay.
	(Tk_DestroyWindow): Check for a null winPtr->mainPtr
	before doing certain cleanup tasks so the we can
	invoke Tk_DestroyWindow on clipboard and send windows.
	We need to do this so that XDestroyIC will get invoked
	for the input contexts of each window.
	* mac/tkMacXStubs.c (TkpCloseDisplay): Don't free
	the displayPtr since this is now done in TkCloseDisplay.
	* unix/tkUnixEvent.c (TkpCloseDisplay, OpenIM): Remove
	conditional compilation around calls to XCloseIM
	since I am confident that the crashes related to
	input contexts has been fixed. Don't free
	the displayPtr since this is now done in TkCloseDisplay.
	* unix/tkUnixSend.c (TkSendCleanup): Invoke the
	Tk_DestroyWindow method to cleanup the special
	send window. This will call XDestroyIC and thereby
	avoid a crash in XCloseIM. The send window needs
	to be Tcl_Preserve and Tcl_Release to avoid an
	invalid memory ref on shutdown.
	* win/tkWinX.c (TkpCloseDisplay): Don't free
	the displayPtr since this is now done in TkCloseDisplay.

Index: generic/tkBind.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/generic/tkBind.c,v
retrieving revision 1.24
diff -u -r1.24 tkBind.c
--- generic/tkBind.c	15 Jun 2002 02:15:51 -0000	1.24
+++ generic/tkBind.c	19 Jun 2002 01:38:16 -0000
@@ -1907,6 +1907,13 @@
     BindInfo *bindInfoPtr;
     PendingBinding *curPtr;
 
+    /*
+     * Certain special windows like those used for send and clipboard
+     * have no mainPtr.
+     */
+    if (winPtr->mainPtr == NULL)
+        return;
+
     bindInfoPtr = (BindInfo *) winPtr->mainPtr->bindInfo;
     curPtr = bindInfoPtr->pendingList;
     while (curPtr != NULL) {
Index: generic/tkClipboard.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/generic/tkClipboard.c,v
retrieving revision 1.9
diff -u -r1.9 tkClipboard.c
--- generic/tkClipboard.c	12 Apr 2002 10:03:02 -0000	1.9
+++ generic/tkClipboard.c	19 Jun 2002 01:38:16 -0000
@@ -649,12 +649,9 @@
 		dispPtr->applicationAtom);
 	Tk_DeleteSelHandler(dispPtr->clipWindow, dispPtr->clipboardAtom,
 		dispPtr->windowAtom);
-	/*
-	 * It may be too late to call Tk_DestroyWindow, so just free the
-	 * memory created directly.
-	 */
-	ckfree((char *) dispPtr->clipWindow);
-	dispPtr->clipWindow = NULL;
+
+	Tk_DestroyWindow(dispPtr->clipWindow);
+	Tcl_Release((ClientData) dispPtr->clipWindow);
     }
 }
 
@@ -700,6 +697,7 @@
     if (dispPtr->clipWindow == NULL) {
 	return TCL_ERROR;
     }
+    Tcl_Preserve((ClientData) dispPtr->clipWindow);
     atts.override_redirect = True;
     Tk_ChangeWindowAttributes(dispPtr->clipWindow, CWOverrideRedirect, &atts);
     Tk_MakeWindowExist(dispPtr->clipWindow);
Index: generic/tkEvent.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/generic/tkEvent.c,v
retrieving revision 1.15
diff -u -r1.15 tkEvent.c
--- generic/tkEvent.c	15 Jun 2002 02:08:12 -0000	1.15
+++ generic/tkEvent.c	19 Jun 2002 01:38:16 -0000
@@ -888,6 +888,8 @@
 
 		    preedit_attr = XVaCreateNestedList(0, XNSpotLocation,
 			    &spot, XNFontSet, dispPtr->inputXfs, NULL);
+		    if (winPtr->inputContext != NULL)
+		        panic("inputContext not NULL");
 		    winPtr->inputContext = XCreateIC(dispPtr->inputMethod,
 			    XNInputStyle, XIMPreeditPosition|XIMStatusNothing,
 			    XNClientWindow, winPtr->window,
@@ -897,6 +899,8 @@
 		    XFree(preedit_attr);
 		} else
 #endif
+		    if (winPtr->inputContext != NULL)
+		        panic("inputContext not NULL");
 		    winPtr->inputContext = XCreateIC(dispPtr->inputMethod,
 			    XNInputStyle, XIMPreeditNothing|XIMStatusNothing,
 			    XNClientWindow, winPtr->window,
Index: generic/tkFocus.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/generic/tkFocus.c,v
retrieving revision 1.8
diff -u -r1.8 tkFocus.c
--- generic/tkFocus.c	14 Jun 2002 22:25:12 -0000	1.8
+++ generic/tkFocus.c	19 Jun 2002 01:38:16 -0000
@@ -813,6 +813,14 @@
     TkDisplay *dispPtr = winPtr->dispPtr;
 
     /*
+     * Certain special windows like those used for send and clipboard
+     * have no mainPtr.
+     */
+
+    if (winPtr->mainPtr == NULL)
+        return;
+
+    /*
      * Search for focus records that refer to this window either as
      * the top-level window or the current focus window.
      */
Index: generic/tkOption.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/generic/tkOption.c,v
retrieving revision 1.13
diff -u -r1.13 tkOption.c
--- generic/tkOption.c	15 Jun 2002 21:02:20 -0000	1.13
+++ generic/tkOption.c	19 Jun 2002 01:38:16 -0000
@@ -787,7 +787,7 @@
      * database.
      */
 
-    if ((winPtr->mainPtr->winPtr == winPtr)
+    if ((winPtr->mainPtr != NULL) && (winPtr->mainPtr->winPtr == winPtr)
 	    && (winPtr->mainPtr->optionRootPtr != NULL)) {
 	ClearOptionTree(winPtr->mainPtr->optionRootPtr);
 	winPtr->mainPtr->optionRootPtr = NULL;
Index: generic/tkWindow.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/generic/tkWindow.c,v
retrieving revision 1.49
diff -u -r1.49 tkWindow.c
--- generic/tkWindow.c	18 Jun 2002 23:51:46 -0000	1.49
+++ generic/tkWindow.c	19 Jun 2002 01:38:18 -0000
@@ -240,8 +240,6 @@
 	ckfree(dispPtr->name);
     }
 
-    Tcl_DeleteHashTable(&dispPtr->winTable);
-
     if (dispPtr->atomInit) {
 	Tcl_DeleteHashTable(&dispPtr->nameTable);
 	Tcl_DeleteHashTable(&dispPtr->atomTable);
@@ -263,6 +261,15 @@
     TkpCloseDisplay(dispPtr);
 
     /*
+     * Delete winTable after TkpCloseDisplay since special windows
+     * may need call Tk_DestroyWindow and it checks the winTable.
+     */
+
+    Tcl_DeleteHashTable(&dispPtr->winTable);
+
+    ckfree((char *) dispPtr);
+
+    /*
      * There is more to clean up, we leave it at this for the time being.
      */
 }
@@ -1262,7 +1269,7 @@
      * can be closed and its data structures deleted.
      */
 
-    if (winPtr->mainPtr->winPtr == winPtr) {
+    if (winPtr->mainPtr != NULL && winPtr->mainPtr->winPtr == winPtr) {
         dispPtr->refCount--;
 	if (tsdPtr->mainWindowList == winPtr->mainPtr) {
 	    tsdPtr->mainWindowList = winPtr->mainPtr->nextPtr;
@@ -1388,6 +1395,7 @@
 #ifdef TK_USE_INPUT_METHODS
     if (winPtr->inputContext != NULL) {
 	XDestroyIC(winPtr->inputContext);
+	winPtr->inputContext = NULL;
     }
 #endif /* TK_USE_INPUT_METHODS */
     if (winPtr->tagPtr != NULL) {
Index: mac/tkMacXStubs.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/mac/tkMacXStubs.c,v
retrieving revision 1.14
diff -u -r1.14 tkMacXStubs.c
--- mac/tkMacXStubs.c	17 Jun 2002 20:09:01 -0000	1.14
+++ mac/tkMacXStubs.c	19 Jun 2002 01:38:19 -0000
@@ -184,7 +184,6 @@
         ckfree((char *) display->screens);
     }
     ckfree((char *) display);
-    ckfree((char *) displayPtr);
 }
 
 /*
Index: unix/tkUnixEvent.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/unix/tkUnixEvent.c,v
retrieving revision 1.8
diff -u -r1.8 tkUnixEvent.c
--- unix/tkUnixEvent.c	15 Jun 2002 01:54:09 -0000	1.8
+++ unix/tkUnixEvent.c	19 Jun 2002 01:38:19 -0000
@@ -172,24 +172,23 @@
 	XFreeFontSet(dispPtr->display, dispPtr->inputXfs);
     }
 #endif
-#if ! defined(SOLARIS2) || defined(HAVE_X11R6)
     if (dispPtr->inputMethod) {
 	/*
-	 * This causes core dumps on some systems (e.g. Solaris 2.3 as of
-	 * 1/6/95), but is OK with X11R6
+	 * This caused core dumps on some systems (Solaris 2.3 1/6/95).
+	 * The most likely cause of this is a bug in X that accesses
+	 * memory that was already deallocated inside XCloseIM().
+	 * One can work around this issue by making sure a XDestroyIC()
+	 * gets invoked for each XCreateIC().
 	 */
 	XCloseIM(dispPtr->inputMethod);
     }
 #endif
-#endif
 
     if (dispPtr->display != 0) {
 	Tcl_DeleteFileHandler(ConnectionNumber(dispPtr->display));
 	(void) XSync(dispPtr->display, False);
 	(void) XCloseDisplay(dispPtr->display);
     }
-
-    ckfree((char *) dispPtr);
 }
 
 /*
@@ -610,12 +609,10 @@
 
     if (dispPtr->inputMethod) {
 	/*
-	 * This causes core dumps on some systems (e.g. Solaris 2.3 as of
-	 * 1/6/95), but is OK with X11R6
+	 * This call should not suffer from any core dumping problems
+	 * since we have not allocated any input contexts.
 	 */
-#if ! defined (SOLARIS2) || defined (HAVE_X11R6)
 	XCloseIM(dispPtr->inputMethod);
-#endif
 	dispPtr->inputMethod = NULL;
     }
 }
Index: unix/tkUnixSend.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/unix/tkUnixSend.c,v
retrieving revision 1.7
diff -u -r1.7 tkUnixSend.c
--- unix/tkUnixSend.c	17 Jun 2002 19:42:11 -0000	1.7
+++ unix/tkUnixSend.c	19 Jun 2002 01:38:20 -0000
@@ -1275,25 +1275,11 @@
 TkSendCleanup(dispPtr)
     TkDisplay *dispPtr;
 {
-    TkWindow *winPtr = (TkWindow *) dispPtr->commTkwin;
-
     if (dispPtr->commTkwin != NULL) {
-	Tk_DeleteEventHandler((Tk_Window) winPtr, PropertyChangeMask,
+	Tk_DeleteEventHandler(dispPtr->commTkwin, PropertyChangeMask,
 		SendEventProc, (ClientData) dispPtr);
-
-	/*
-	 * We need to manually free all the XIC structures that
-	 * have been allocated in order to avoid a nasty bug in XCloseIM().
-	 */
-	if (winPtr->inputContext != NULL) {
-	    XDestroyIC(winPtr->inputContext);
-	    winPtr->inputContext = NULL;
-	}
-
-#ifdef PURIFY
-	/* Tk_DestroyWindow(dispPtr->commTkwin); */
-	ckfree((char *) dispPtr->commTkwin);
-#endif
+	Tk_DestroyWindow(dispPtr->commTkwin);
+	Tcl_Release((ClientData) dispPtr->commTkwin);
 	dispPtr->commTkwin = NULL;
     }
 }
@@ -1335,6 +1321,7 @@
     if (dispPtr->commTkwin == NULL) {
 	panic("Tk_CreateWindow failed in SendInit!");
     }
+    Tcl_Preserve((ClientData) dispPtr->commTkwin);
     atts.override_redirect = True;
     Tk_ChangeWindowAttributes(dispPtr->commTkwin,
 	    CWOverrideRedirect, &atts);
Index: win/tkWinX.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/win/tkWinX.c,v
retrieving revision 1.22
diff -u -r1.22 tkWinX.c
--- win/tkWinX.c	17 Jun 2002 20:09:01 -0000	1.22
+++ win/tkWinX.c	19 Jun 2002 01:38:21 -0000
@@ -558,7 +558,6 @@
         ckfree((char *) display->screens);
     }
     ckfree((char *) display);
-    ckfree((char *) dispPtr);
 }
 
 /*