From 3714b71e80ff510478768fcd7b95c6426a6f99b3 Mon Sep 17 00:00:00 2001 From: Marja Hassinen Date: Fri, 10 Sep 2010 15:46:24 +0300 Subject: Lame attempts to "fix" the qAddPostRoutine problems. qAddPostRoutine problems: when libcontextsubscriber is unloaded, QCoreApplication will still contain a reference to a function in it, and it will crash. --- libcontextsubscriber/src/infobackend.cpp | 19 ++++++++++++++++++- libcontextsubscriber/src/infobackend.h | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/libcontextsubscriber/src/infobackend.cpp b/libcontextsubscriber/src/infobackend.cpp index 419cd780..614c90e6 100644 --- a/libcontextsubscriber/src/infobackend.cpp +++ b/libcontextsubscriber/src/infobackend.cpp @@ -72,6 +72,15 @@ InfoBackend* InfoBackend::instance(const QString &backendName) // Move the backend to the main thread backendInstance->moveToThread(QCoreApplication::instance()->thread()); + // We must ensure that: + // 1) QFileSystemWatcher is deleted before QCoreApplication. + // 2) libcontextsubscriber can be unloaded successfully + // (QCoreApplication cannot have a post routine that is inside a library + // that has been unloaded.) + + // For 1), we add a post routine to QCoreApplication for deleting the + // QFileSystemWathcer, for 2) we remove the post routine and delete the + // QFileSystemWatcher when the library is unloaded. qAddPostRoutine(destroyInstance); } @@ -149,6 +158,14 @@ void InfoBackend::disconnectNotify(const char *signal) void InfoBackend::destroyInstance() { delete backendInstance; - backendInstance = NULL; + backendInstance = 0; } +/// This will be called when the library is unloaded. +void __attribute__((destructor)) library_dtor() +{ + // It seems to be ok to call qRemovePostRoutine even if QCoreApplication + // doesn't exist any more. + qRemovePostRoutine(InfoBackend::destroyInstance); + InfoBackend::destroyInstance(); +} diff --git a/libcontextsubscriber/src/infobackend.h b/libcontextsubscriber/src/infobackend.h index cc4d7c90..ff37e1e6 100644 --- a/libcontextsubscriber/src/infobackend.h +++ b/libcontextsubscriber/src/infobackend.h @@ -37,6 +37,7 @@ class InfoBackend : public QObject public: static InfoBackend* instance(const QString &backendName = ""); + static void destroyInstance(); /// Returns the name of the backend, ie: 'xml'. virtual QString name() const = 0; @@ -95,7 +96,6 @@ private: InfoBackend& operator=(const InfoBackend&); static InfoBackend* backendInstance; ///< Holds a pointer to the instance of the singelton. - static void destroyInstance(); friend class InfoXmlBackend; friend class InfoCdbBackend; -- cgit v1.2.3