[PATCH] Fix for bug 147036 (KGlobalAccel dtor crashes during app shutdown)

Michael Pyne michael.pyne at kdemail.net
Sun Jun 24 06:35:17 BST 2007


Hi all,

Could someone review the following patch for bug 147036 
(http://bugs.kde.org/show_bug.cgi?id=147036) ?

Basically the problem is that during application shutdown, the static deleter 
for KGlobalAccel fires and starts the shutdown process.  Which is good, but 
that involves releasing the grab it has on various keys, which (in 
kkeyserver_x11.cpp) ends up calling QX11Info::display().

But by this point the Display handle has been closed (so ::display() returns 
0), so libX11 crashes.  Even if you cache the value, it's not longer valid so 
libX11 still crashes.

The fix is either to force deletion before the GUI is unloaded or to not 
ungrab keys for the static deleter shutdown.  I think this can be a general 
difficulty so I went for adding a method to KApplication to allow 
programs/libraries to add a callback function to be called just before 
KApplication gets destroyed.  I don't want to just commit to KApplication 
though so if anyone has got comments/suggestions please feel free. :)

This fix does work for the crash induced by JuK, I don't have time to test 
kicker but I can't see why it wouldn't have fixed that one as well.

Regards,
 - Michael Pyne

Index: kernel/kapplication.cpp
===================================================================
--- kernel/kapplication.cpp	(revision 677853)
+++ kernel/kapplication.cpp	(working copy)
@@ -216,6 +216,7 @@
 
   QString sessionKey;
   QString pSessionConfigFile;
+  QList<KApplication::CleanupFunction> cleanupFunctions;
 };
 
 
@@ -618,6 +619,11 @@
     return KApp;
 }
 
+void KApplication::addCleanupFunction(CleanupFunction fn)
+{
+    d->cleanupFunctions.append(fn);
+}
+
 KConfig* KApplication::sessionConfig()
 {
     if (!pSessionConfig) // create an instance specific config object
@@ -889,6 +895,9 @@
       IceSetIOErrorHandler( d->oldIceIOErrorHandler );
 #endif
 
+  foreach(CleanupFunction fn, d->cleanupFunctions)
+    fn();
+
   delete d;
   KApp = 0;
 
Index: kernel/kapplication.h
===================================================================
--- kernel/kapplication.h	(revision 677853)
+++ kernel/kapplication.h	(working copy)
@@ -151,6 +151,20 @@
    */
   KConfig* sessionConfig();
 
+  typedef void (*CleanupFunction)(); ///< Defines a cleanup function.
+
+  /**
+   * Adds a function to run before de-initializing the GUI.  Use this to
+   * call destructors or other cleanup functions that must run while the GUI
+   * is still running.  Note that this is called from the destructor so you
+   * cannot rely on any windows still being open or the existence of 
siblings.
+   * The order that cleanup functions are called in is undefined, except that
+   * it happens while the GUI is still active.
+   *
+   * @param fn The function to call before GUI de-initialization.
+   */
+  void addCleanupFunction(CleanupFunction fn);
+
 #ifdef KDE3_SUPPORT
  /**
    * Is the application restored from the session manager?
Index: shortcuts/kglobalaccel.cpp
===================================================================
--- shortcuts/kglobalaccel.cpp	(revision 679485)
+++ shortcuts/kglobalaccel.cpp	(working copy)
@@ -43,6 +43,7 @@
 #include "kmessagebox.h"
 #include <kconfig.h>
 #include <kconfiggroup.h>
+#include <kapplication.h>
 
 
 class KGlobalAccelPrivate
@@ -187,6 +188,7 @@
 KGlobalAccel * KGlobalAccel::self( )
 {
     K_GLOBAL_STATIC(KGlobalAccel, s_instance)
+    KApplication::kApplication()->addCleanupFunction(s_instance.destroy);
     return s_instance;
 }
 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20070624/bd96b14f/attachment.sig>


More information about the kde-core-devel mailing list