summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Rauwolf <rauwolf@itestra.de>2013-03-11 15:12:26 +0100
committerPhilip Rauwolf <rauwolf@itestra.de>2013-03-11 15:12:26 +0100
commitbb4f611e20cc095b6799e7061c3a0b501445c479 (patch)
tree734afc75de81521ce9e7640c47364bbd80c66156
parent8f8c18821eda6976719256c03b502e40459079b9 (diff)
downloadgenivi-common-api-dbus-runtime-bb4f611e20cc095b6799e7061c3a0b501445c479.tar.gz
Prevented dispatch mechanism from returning into an undefined
(already destructed) environment.
-rw-r--r--src/CommonAPI/DBus/DBusConnection.cpp28
-rw-r--r--src/CommonAPI/DBus/DBusConnection.h2
2 files changed, 13 insertions, 17 deletions
diff --git a/src/CommonAPI/DBus/DBusConnection.cpp b/src/CommonAPI/DBus/DBusConnection.cpp
index d151854..fa712bb 100644
--- a/src/CommonAPI/DBus/DBusConnection.cpp
+++ b/src/CommonAPI/DBus/DBusConnection.cpp
@@ -29,9 +29,17 @@ DBusObjectPathVTable DBusConnection::libdbusObjectPathVTable_ = {
&DBusConnection::onLibdbusObjectPathMessageThunk
};
-void DBusConnection::dispatch() {
- while (!stopDispatching_ && readWriteDispatch(10)) {
+void DBusConnection::dispatch(std::shared_ptr<DBusConnection> selfReference) {
+ while (!stopDispatching_ && readWriteDispatch(10) && (selfReference.use_count() > 1)) {}
+}
+
+bool DBusConnection::readWriteDispatch(int timeoutMilliseconds) {
+ if(isConnected()) {
+ const dbus_bool_t libdbusSuccess = dbus_connection_read_write_dispatch(libdbusConnection_,
+ timeoutMilliseconds);
+ return libdbusSuccess;
}
+ return false;
}
DBusConnection::DBusConnection(BusType busType) :
@@ -51,9 +59,7 @@ DBusConnection::DBusConnection(::DBusConnection* libDbusConnection) :
}
DBusConnection::~DBusConnection() {
- if (isConnected()) {
- disconnect();
- }
+ disconnect();
}
bool DBusConnection::connect() {
@@ -82,7 +88,7 @@ bool DBusConnection::connect(DBusError& dbusError) {
initLibdbusSignalFilterAfterConnect();
stopDispatching_ = false;
- dispatchThread_ = std::thread(std::bind(&DBusConnection::dispatch, this));
+ dispatchThread_ = std::thread(&DBusConnection::dispatch, this, this->shared_from_this());
dbusConnectionStatusEvent_.notifyListeners(AvailabilityStatus::AVAILABLE);
@@ -257,16 +263,6 @@ DBusMessage DBusConnection::sendDBusMessageWithReplyAndBlock(const DBusMessage&
return DBusMessage(libdbusMessageReply, increaseLibdbusMessageReferenceCount);
}
-
-bool DBusConnection::readWriteDispatch(int timeoutMilliseconds) {
- if(isConnected()) {
- const dbus_bool_t libdbusSuccess = dbus_connection_read_write_dispatch(libdbusConnection_,
- timeoutMilliseconds);
- return libdbusSuccess;
- }
- return false;
-}
-
DBusProxyConnection::DBusSignalHandlerToken DBusConnection::addSignalMemberHandler(const std::string& objectPath,
const std::string& interfaceName,
const std::string& interfaceMemberName,
diff --git a/src/CommonAPI/DBus/DBusConnection.h b/src/CommonAPI/DBus/DBusConnection.h
index a9c1044..44f9afe 100644
--- a/src/CommonAPI/DBus/DBusConnection.h
+++ b/src/CommonAPI/DBus/DBusConnection.h
@@ -99,7 +99,7 @@ class DBusConnection: public DBusProxyConnection, public std::enable_shared_from
virtual const std::shared_ptr<DBusObjectManager> getDBusObjectManager();
private:
- void dispatch();
+ void dispatch(std::shared_ptr<DBusConnection> selfReference);
std::thread dispatchThread_;
bool stopDispatching_;