IoTivity instance signleton memory allocation changed from dynamic to static.
authorLomtev Dmytro <d.lomtev@samsung.com>
Tue, 5 Sep 2017 06:35:29 +0000 (09:35 +0300)
committerLomtev Dmytro <d.lomtev@samsung.com>
Tue, 5 Sep 2017 06:35:29 +0000 (09:35 +0300)
device_core/iotivity_lib/inc/iotivity.h
device_core/iotivity_lib/src/iotivity.cpp

index 69687ac..0d49dea 100644 (file)
@@ -242,7 +242,7 @@ static void connectionChangedCallback(const std::string& url, int con_type, bool
 IoTivity();
 ~IoTivity();
 
-static IoTivity* instance;
+static IoTivity instance;
 /**
  * Path to persistent storage to get device id.
  * Default depends on -DPS_PATH definition.
index 93fff13..058c6ce 100644 (file)
@@ -42,7 +42,7 @@ namespace NetworkManager
 const std::string IoTivity::DEFAULT_PROVIDER = "Samsung";
 const int IoTivity::DEFAULT_TIMEOUT = 3;
 
-IoTivity* IoTivity::instance = nullptr;
+IoTivity IoTivity::instance;
 
 static FILE* openPersistentStorage(const char* ps_path, const char* mode)
 {
@@ -75,18 +75,17 @@ void IoTivity::connectionChangedCallback(const std::string& conn, /*OCConnectivi
     LOG_D(TAG, "Connection Changed connection[%s], type = %s, state = %s", conn.c_str(),
           getConType((OCConnectivityType)con_type).c_str(), state ? "true" : "false");
 
-    if (nullptr != instance) {
-        std::unique_lock<std::mutex> lock(instance->instance_mutex);
+    IoTivity* iotivity = IoTivity::getInstance();
+    std::unique_lock<std::mutex> lock(iotivity->instance_mutex);
 
-        if (instance->cloud_host.find(conn) != std::string::npos) {
-            LOG_I(TAG, "Cloud connection changed");
-            if (!state) {
-                LOG_D(TAG, "Connection losts.");
-//                instance->connected = false;
-            }
-
-            instance->connected = state;
+    if (iotivity->cloud_host.find(conn) != std::string::npos) {
+        LOG_I(TAG, "Cloud connection changed");
+        if (!state) {
+            LOG_D(TAG, "Connection losts.");
+//            iotivity->connected = false;
         }
+
+        iotivity->connected = state;
     }
 }
 
@@ -97,46 +96,38 @@ IoTivity::IoTivity(): cloud_host(), account_mgr(nullptr), presence_handle(nullpt
     if (OC_STACK_OK != res) {
         LOG_E(TAG, "CAManager::setNetworkMonitorHandler failed with code = %d", res);
     }
+
+    OC::OCPlatform::Configure(IoTivity::platform_config);
+    OC::OCPlatform::start();
 }
 
 IoTivity::~IoTivity()
 {
     FN_VISIT
     signOut();
+    CAManager::unsetNetworkMonitorHandler();
     OC::OCPlatform::stop();
 }
 
 IoTivity* IoTivity::getInstance()
 {
-    if (instance == nullptr) {
-        try {
-            instance = new IoTivity;
-            OC::OCPlatform::Configure(IoTivity::platform_config);
-            OC::OCPlatform::start();
-        } catch (std::exception& e) {
-            cleanUp();
-            throw std::runtime_error("Out of memory");
-        }
-    }
-    return instance;
+    return &instance;
 }
 
 IMqClient* IoTivity::getMqHandler()
 {
     IMqClient* handler = mq_handler.get();
+
     if (handler == nullptr) {
         LOG_W(TAG, "getMqHandler(): MqHandler uninitialized");
         throw std::runtime_error("Failed to connect to message queue");
     }
+
     return handler;
 }
 
 void IoTivity::cleanUp()
 {
-    if (instance != nullptr) {
-        delete instance;
-        instance = nullptr;
-    }
 }
 
 void IoTivity::registerDeviceInfo(const std::string& name, const std::string& model, const std::string& type)
@@ -570,9 +561,7 @@ void IoTivity::unPublishAllResources()
 
 void IoTivity::setPersistentStoragePath(std::string newPath)
 {
-    if (instance != nullptr) {
-        instance->device_id.clear();
-    }
+    instance.device_id.clear();
     persistent_storage_path = newPath;
 }