diff -rwu tk8.6.6/generic/tk.h tk8.6.6-new/generic/tk.h
--- tk8.6.6/generic/tk.h	2016-07-22 05:11:20.000000000 -0700
+++ tk8.6.6-new/generic/tk.h	2016-12-13 14:25:47.682118771 -0800
@@ -794,6 +794,7 @@
     unsigned int flags;
     char *dummy8;		/* handlerList */
 #ifdef TK_USE_INPUT_METHODS
+    int dummy9a;
     XIC dummy9;			/* inputContext */
 #endif /* TK_USE_INPUT_METHODS */
     ClientData *dummy10;	/* tagPtr */
diff -rwu tk8.6.6/generic/tkEvent.c tk8.6.6-new/generic/tkEvent.c
--- tk8.6.6/generic/tkEvent.c	2016-07-22 05:11:20.000000000 -0700
+++ tk8.6.6-new/generic/tkEvent.c	2016-12-13 14:37:36.588217984 -0800
@@ -356,6 +356,7 @@
 	/* XCreateIC failed. */
 	return;
     }
+    winPtr->ximGeneration = dispPtr->ximGeneration;

     /*
      * Adjust the window's event mask if the IM requires it.
@@ -1216,6 +1217,7 @@
     unsigned long mask;
     InProgress ip;
     Tcl_Interp *interp = NULL;
+
     ThreadSpecificData *tsdPtr =
 	    Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));

@@ -1288,6 +1290,14 @@
      */

 #ifdef TK_USE_INPUT_METHODS
+    /*
+     * If the XIC has been invalidated, it must be recreated.
+     */
+    if (winPtr->dispPtr->ximGeneration != winPtr->ximGeneration) {
+      winPtr->flags &= ~TK_CHECKED_IC;
+      winPtr->inputContext = NULL;
+    }
+
     if ((winPtr->dispPtr->flags & TK_DISPLAY_USE_IM)) {
 	if (!(winPtr->flags & (TK_CHECKED_IC|TK_ALREADY_DEAD))) {
 	    winPtr->flags |= TK_CHECKED_IC;
@@ -1295,7 +1305,9 @@
 		CreateXIC(winPtr);
 	    }
 	}
-	if (eventPtr->type == FocusIn && winPtr->inputContext != NULL) {
+	if (eventPtr->type == FocusIn &&
+              winPtr->dispPtr->inputMethod != NULL &&
+              winPtr->inputContext != NULL) {
 	    XSetICFocus(winPtr->inputContext);
 	}
     }
diff -rwu tk8.6.6/generic/tkInt.h tk8.6.6-new/generic/tkInt.h
--- tk8.6.6/generic/tkInt.h	2016-07-22 05:11:20.000000000 -0700
+++ tk8.6.6-new/generic/tkInt.h	2016-12-13 14:18:21.771808127 -0800
@@ -472,6 +472,7 @@
      */

 #ifdef TK_USE_INPUT_METHODS
+    int ximGeneration;          /* Used to invalidate XIC */
     XIM inputMethod;		/* Input method for this display. */
     XIMStyle inputStyle;	/* Input style selected for this display. */
     XFontSet inputXfs;		/* XFontSet cached for over-the-spot XIM. */
@@ -728,6 +729,7 @@
     TkEventHandler *handlerList;/* First in list of event handlers declared
 				 * for this window, or NULL if none. */
 #ifdef TK_USE_INPUT_METHODS
+    int ximGeneration;          /* Used to invalidate XIC */
     XIC inputContext;		/* XIM input context. */
 #endif /* TK_USE_INPUT_METHODS */

diff -rwu tk8.6.6/generic/tkWindow.c tk8.6.6-new/generic/tkWindow.c
--- tk8.6.6/generic/tkWindow.c	2016-07-22 05:11:20.000000000 -0700
+++ tk8.6.6-new/generic/tkWindow.c	2016-12-13 14:32:44.768050234 -0800
@@ -355,6 +355,9 @@
      * Set the flags specified in the call.
      */

+#ifdef TK_USE_INPUT_METHODS
+    winPtr->ximGeneration = 0;
+#endif /*TK_USE_INPUT_METHODS*/
     winPtr->flags |= flags;

     /*
@@ -472,6 +475,9 @@
 	    dispPtr->cursorFont = None;
 	    dispPtr->warpWindow = NULL;
 	    dispPtr->multipleAtom = None;
+#ifdef TK_USE_INPUT_METHODS
+            dispPtr->ximGeneration = 0;
+#endif /*TK_USE_INPUT_METHODS*/

 	    /*
 	     * By default we do want to collapse motion events in
@@ -1442,10 +1448,11 @@
     UnlinkWindow(winPtr);
     TkEventDeadWindow(winPtr);
 #ifdef TK_USE_INPUT_METHODS
-    if (winPtr->inputContext != NULL) {
+    if (winPtr->inputContext != NULL &&
+           winPtr->ximGeneration == winPtr->dispPtr->ximGeneration) {
 	XDestroyIC(winPtr->inputContext);
-	winPtr->inputContext = NULL;
     }
+    winPtr->inputContext = NULL;
 #endif /* TK_USE_INPUT_METHODS */
     if (winPtr->tagPtr != NULL) {
 	TkFreeBindingTags(winPtr);
diff -rwu tk8.6.6/unix/tkUnixEvent.c tk8.6.6-new/unix/tkUnixEvent.c
--- tk8.6.6/unix/tkUnixEvent.c	2016-07-22 05:11:21.000000000 -0700
+++ tk8.6.6-new/unix/tkUnixEvent.c	2016-12-13 14:43:00.004836606 -0800
@@ -38,6 +38,8 @@
 static void		DisplaySetupProc(ClientData clientData, int flags);
 static void		TransferXEventsToTcl(Display *display);
 #ifdef TK_USE_INPUT_METHODS
+static void             InstantiateIMCallback(Display *, XPointer client_data, XPointer call_data);
+static void             DestroyIMCallback(XIM im, XPointer client_data, XPointer call_data);
 static void		OpenIM(TkDisplay *dispPtr);
 #endif
 
@@ -179,6 +181,8 @@
     dispPtr->flags |= use_xkb;
 #ifdef TK_USE_INPUT_METHODS
     OpenIM(dispPtr);
+    XRegisterIMInstantiateCallback(dispPtr->display, NULL, NULL, NULL,
+        InstantiateIMCallback, (XPointer) dispPtr);
 #endif
     Tcl_CreateFileHandler(ConnectionNumber(display), TCL_READABLE,
 	    DisplayFileProc, dispPtr);
@@ -664,6 +668,36 @@
 }
 #ifdef TK_USE_INPUT_METHODS
 
+static void
+InstantiateIMCallback(
+   Display      *display,
+   XPointer     client_data,
+   XPointer     call_data)
+{
+   TkDisplay    *dispPtr;
+
+   dispPtr = (TkDisplay *) client_data;
+   OpenIM(dispPtr);
+   XUnregisterIMInstantiateCallback(dispPtr->display, NULL, NULL, NULL,
+        InstantiateIMCallback, (XPointer) dispPtr);
+}
+
+static void
+DestroyIMCallback(
+    XIM         im,
+    XPointer    client_data,
+    XPointer    call_data)
+{
+    TkDisplay   *dispPtr;
+
+    dispPtr = (TkDisplay *) client_data;
+    dispPtr->inputMethod = NULL;
+    ++dispPtr->ximGeneration;
+    XRegisterIMInstantiateCallback(dispPtr->display, NULL, NULL, NULL,
+        InstantiateIMCallback, (XPointer) dispPtr);
+}
+
+
 /*
  *--------------------------------------------------------------
  *
@@ -693,11 +727,23 @@
 	return;
     }

+    ++dispPtr->ximGeneration;
     dispPtr->inputMethod = XOpenIM(dispPtr->display, NULL, NULL, NULL);
     if (dispPtr->inputMethod == NULL) {
 	return;
     }

+    /* Require X11R6 */
+    {
+        XIMCallback destroy_cb;
+
+        destroy_cb.callback = DestroyIMCallback;
+        destroy_cb.client_data = (XPointer) dispPtr;
+        if (XSetIMValues(dispPtr->inputMethod, XNDestroyCallback,
+              &destroy_cb, NULL))
+           goto error;
+    }
+
     if ((XGetIMValues(dispPtr->inputMethod, XNQueryInputStyle, &stylePtr,
 	    NULL) != NULL) || (stylePtr == NULL)) {
 	goto error;
@@ -744,6 +790,7 @@
     if (dispPtr->inputMethod) {
 	XCloseIM(dispPtr->inputMethod);
 	dispPtr->inputMethod = NULL;
+        ++dispPtr->ximGeneration;
     }
 }
 #endif /* TK_USE_INPUT_METHODS */
