From 02cb73a67f56fd5ac39fae4ea7b98932edb3469e Mon Sep 17 00:00:00 2001 From: Lomtev Dmytro Date: Tue, 5 Sep 2017 09:35:29 +0300 Subject: [PATCH] IoTivity instance signleton memory allocation changed from dynamic to static. --- device_core/iotivity_lib/inc/iotivity.h | 2 +- device_core/iotivity_lib/src/iotivity.cpp | 47 ++++++++++++------------------- 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/device_core/iotivity_lib/inc/iotivity.h b/device_core/iotivity_lib/inc/iotivity.h index 69687ac..0d49dea 100644 --- a/device_core/iotivity_lib/inc/iotivity.h +++ b/device_core/iotivity_lib/inc/iotivity.h @@ -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. diff --git a/device_core/iotivity_lib/src/iotivity.cpp b/device_core/iotivity_lib/src/iotivity.cpp index 93fff13..058c6ce 100644 --- a/device_core/iotivity_lib/src/iotivity.cpp +++ b/device_core/iotivity_lib/src/iotivity.cpp @@ -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 lock(instance->instance_mutex); + IoTivity* iotivity = IoTivity::getInstance(); + std::unique_lock 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; } -- 2.7.4