From: Po Lu <luangruo@yahoo.com>
Date: Sun, 30 Oct 2022 05:13:28 +0000 (+0800)
Subject: Plug big leaks upon display IO error
X-Git-Tag: emacs-29.0.90~1616^2~386
X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=ae0ec1eede3dac8ae82c3a82d7d7f6b956d8db24;p=emacs.git

Plug big leaks upon display IO error

Frame GCs are still leaked, but there is nothing that can be
done in that case because the display connection is dead.

* src/xterm.c (x_delete_terminal): Always free xkb desc and
modmap.
---

diff --git a/src/xterm.c b/src/xterm.c
index f1bccddb6c3..7dd969b821f 100644
--- a/src/xterm.c
+++ b/src/xterm.c
@@ -30277,11 +30277,6 @@ x_delete_terminal (struct terminal *terminal)
 	 closing all the displays.  */
       XrmDestroyDatabase (dpyinfo->rdb);
 #endif
-
-#ifdef HAVE_XKB
-      if (dpyinfo->xkb_desc)
-	XkbFreeKeyboard (dpyinfo->xkb_desc, XkbAllComponentsMask, True);
-#endif
 #ifdef USE_GTK
       xg_display_close (dpyinfo->display);
 #else
@@ -30291,9 +30286,6 @@ x_delete_terminal (struct terminal *terminal)
       XCloseDisplay (dpyinfo->display);
 #endif
 #endif /* ! USE_GTK */
-
-      if (dpyinfo->modmap)
-	XFreeModifiermap (dpyinfo->modmap);
       /* Do not close the connection here because it's already closed
 	 by X(t)CloseDisplay (Bug#18403).  */
       dpyinfo->display = NULL;
@@ -30306,6 +30298,18 @@ x_delete_terminal (struct terminal *terminal)
   else if (dpyinfo->connection >= 0)
     emacs_close (dpyinfo->connection);
 
+  /* Free the keyboard and modifier maps here; that is safe to do
+     without a display, and not doing so leads to a lot of data being
+     leaked upon IO error.  */
+
+#ifdef HAVE_XKB
+  if (dpyinfo->xkb_desc)
+    XkbFreeKeyboard (dpyinfo->xkb_desc, XkbAllComponentsMask, True);
+#endif
+
+  if (dpyinfo->modmap)
+    XFreeModifiermap (dpyinfo->modmap);
+
   /* No more input on this descriptor.  */
   delete_keyboard_wait_descriptor (dpyinfo->connection);
   /* Mark as dead. */