diff options
author | Holger Schröder <holger.schroeder.ext@basyskom.de> | 2011-01-19 17:36:05 +0100 |
---|---|---|
committer | Holger Schröder <holger.schroeder.ext@basyskom.de> | 2011-01-19 18:03:09 +0100 |
commit | fb56abb143616105032ac7f33d0bcfdff8e8dd24 (patch) | |
tree | cb67fbdd9d29f154a9e5454fcc7181a6238f8ca4 /src | |
parent | cf059a523c0934c50db128dc2ce800e6e93e0046 (diff) |
Fixes: NB#209179 - QDBusConnection created before MApplication instantiation leads to threading issues
RevBy: Armin Berres
Details: According to the qt bugtracker, the behaviour of
QDBusConnection::sessionBus() and QDBusConnection::systemBus()
is undefined, when they are used before the QApplication
construnctor was executed.
Because of this we defer the creation of the sessionBus
until it is accessed the first time. And we do write an
error message when the sessionBus is accessed before
the qApp instance exists.
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/core/mapplicationservice_p.cpp | 42 | ||||
-rw-r--r-- | src/corelib/core/mapplicationservice_p.h | 4 |
2 files changed, 41 insertions, 5 deletions
diff --git a/src/corelib/core/mapplicationservice_p.cpp b/src/corelib/core/mapplicationservice_p.cpp index de64a3fb..8b9eb2bc 100644 --- a/src/corelib/core/mapplicationservice_p.cpp +++ b/src/corelib/core/mapplicationservice_p.cpp @@ -31,7 +31,7 @@ MApplicationServicePrivate::MApplicationServicePrivate(const QString &newService registered(false), instanceCounter(0), q_ptr(NULL), - dBusConnection(QDBusConnection::sessionBus()), + pDBusConnection(NULL), mApp(MApplication::instance()) { } @@ -40,6 +40,37 @@ MApplicationServicePrivate::~MApplicationServicePrivate() { } +static QString getexepath() +{ + char result[ 255 ]; + memset( result, 0, 255 ); + readlink( "/proc/self/exe", result, 254 ); + return QString( result ); +} + +void MApplicationServicePrivate::ensureDBusConnection() +{ + // here we create the dbusconnection only when needed + // the behaviour of a QDBusConnection::sessionBus() + // or QDBusConnection::systemBus() is undefined, when it + // is instantiated before the QApplication constructor + // is finished. + if ( ! pDBusConnection ) + { + if ( ! QCoreApplication::instance() ) + { + qCritical() << "error: MApplicationServicePrivate::ensureDBusConnection" + " without QCoreApplication::instance. pid: " + << getpid() << "appname:" << getexepath(); + } + + static QDBusConnection myDBusConnection = QDBusConnection::sessionBus(); + + // now initialize the connection + pDBusConnection = &myDBusConnection; + } +} + void MApplicationServicePrivate::launchNewProcess(const QString &binaryName, const QStringList &arguments) { QProcess *myProcess = new QProcess(); @@ -53,17 +84,20 @@ void MApplicationServicePrivate::launchNewProcess(const QString &binaryName, con bool MApplicationServicePrivate::registerService(const QString &serviceName) { - return dBusConnection.registerService(serviceName); + ensureDBusConnection(); + return pDBusConnection->registerService(serviceName); } void MApplicationServicePrivate::registerObject(const QString &path, QObject *object) { - dBusConnection.registerObject(path, object); + ensureDBusConnection(); + pDBusConnection->registerObject(path, object); } void MApplicationServicePrivate::unregisterObject(const QString &path) { - dBusConnection.unregisterObject(path); + ensureDBusConnection(); + pDBusConnection->unregisterObject(path); } QString MApplicationServicePrivate::appName() diff --git a/src/corelib/core/mapplicationservice_p.h b/src/corelib/core/mapplicationservice_p.h index 8f622e1b..34190320 100644 --- a/src/corelib/core/mapplicationservice_p.h +++ b/src/corelib/core/mapplicationservice_p.h @@ -59,8 +59,10 @@ protected: bool isPrestarted(); bool activeWindowSet(); + void ensureDBusConnection(); + private: - QDBusConnection dBusConnection; + QDBusConnection *pDBusConnection; MApplication *mApp; }; |