Fixed occasional deadlock that occurred when dbus connection got
[profile/ivi/common-api-dbus-runtime.git] / src / CommonAPI / DBus / DBusServiceRegistry.cpp
index f883797..a54116c 100644 (file)
@@ -16,25 +16,28 @@ namespace DBus {
 DBusServiceRegistry::DBusServiceRegistry(std::shared_ptr<DBusProxyConnection> dbusProxyConnection):
                 dbusDaemonProxy_(std::make_shared<CommonAPI::DBus::DBusDaemonProxy>(dbusProxyConnection)),
                 dbusServicesStatus_(AvailabilityStatus::UNKNOWN) {
+}
+
+DBusServiceRegistry::~DBusServiceRegistry() {
+    dbusDaemonProxy_->getNameOwnerChangedEvent().unsubscribe(dbusDaemonProxyNameOwnerChangedEventSubscription_);
+    dbusDaemonProxy_->getProxyStatusEvent().unsubscribe(dbusDaemonProxyStatusEventSubscription_);
+    std::cout << "Crushing stuff" << std::endl;
+}
 
+void DBusServiceRegistry::init() {
     dbusDaemonProxyStatusEventSubscription_ =
                     dbusDaemonProxy_->getProxyStatusEvent().subscribeCancellableListener(
-                                    std::bind(&DBusServiceRegistry::onDBusDaemonProxyStatusEvent, this, std::placeholders::_1));
+                                    std::bind(&DBusServiceRegistry::onDBusDaemonProxyStatusEvent, this->shared_from_this(), std::placeholders::_1));
 
     dbusDaemonProxyNameOwnerChangedEventSubscription_ =
                     dbusDaemonProxy_->getNameOwnerChangedEvent().subscribeCancellableListener(
                     std::bind(&DBusServiceRegistry::onDBusDaemonProxyNameOwnerChangedEvent,
-                              this,
+                              this->shared_from_this(),
                               std::placeholders::_1,
                               std::placeholders::_2,
                               std::placeholders::_3));
 }
 
-DBusServiceRegistry::~DBusServiceRegistry() {
-    dbusDaemonProxy_->getNameOwnerChangedEvent().unsubscribe(dbusDaemonProxyNameOwnerChangedEventSubscription_);
-    dbusDaemonProxy_->getProxyStatusEvent().unsubscribe(dbusDaemonProxyStatusEventSubscription_);
-}
-
 bool DBusServiceRegistry::waitDBusServicesAvailable(std::unique_lock<std::mutex>& lock, std::chrono::milliseconds& timeout) {
     bool dbusServicesStatusIsKnown = (dbusServicesStatus_ != AvailabilityStatus::UNKNOWN);
 
@@ -324,7 +327,7 @@ SubscriptionStatus DBusServiceRegistry::onDBusDaemonProxyStatusEvent(const Avail
             dbusServicesStatus_ = AvailabilityStatus::UNKNOWN;
             dbusDaemonProxy_->listNamesAsync(std::bind(
                             &DBusServiceRegistry::onListNamesCallback,
-                            this,
+                            this->shared_from_this(),
                             std::placeholders::_1,
                             std::placeholders::_2));
             break;
@@ -478,7 +481,7 @@ void DBusServiceRegistry::resolveDBusServiceInstances(DBusServiceList::iterator&
 
     // search for remote instances
     DBusDaemonProxy::GetManagedObjectsAsyncCallback callback = std::bind(&DBusServiceRegistry::onGetManagedObjectsCallback,
-                                                                         this,
+                                                                         this->shared_from_this(),
                                                                          std::placeholders::_1,
                                                                          std::placeholders::_2,
                                                                          dbusServiceName);