[Iotcon] Fixed wrong resource release in 3 functions 45/88545/4
authorMichal Kolodziej <m.kolodziej@samsung.com>
Mon, 19 Sep 2016 14:41:39 +0000 (16:41 +0200)
committerMichal Kolodziej <m.kolodziej@samsung.com>
Mon, 19 Sep 2016 14:41:39 +0000 (16:41 +0200)
[Details] Fixed resource release in ClientFindResource (was creating new thread without locks),
ClientFindDeviceInfo and ClientFindPlatformInfo (in both cases there is a possibility of multicast,
and shared resources were released on first callback)

Change-Id: Id8295a36f2b72a0d8f7af2d07270c8229f2412d9
Signed-off-by: Michal Kolodziej <m.kolodziej@samsung.com>
src/iotcon/iotcon_instance.cc

index 057c7a554608c275ea6f559089d8ba2f0a898d9f..6f12ded77342545347730634bb16c24fcd97dbba 100644 (file)
@@ -1163,15 +1163,21 @@ bool IotconInstance::ResourceFoundCallback(iotcon_remote_resource_h resource,
     return IOTCON_FUNC_STOP;
   }
 
-  auto ret = IotconUtils::ConvertIotconError(result);
-  if (!ret) {
-    data->fun(ret, picojson::value{});
-    return IOTCON_FUNC_STOP;
+  switch (result) {
+    case IOTCON_ERROR_NONE:
+      break;
+    case IOTCON_ERROR_TIMEOUT:
+      delete data;
+      return IOTCON_FUNC_STOP;
+    default:
+      auto ret = IotconUtils::ConvertIotconError(result);
+      data->fun(ret, picojson::value{});
+      return IOTCON_FUNC_STOP;
   }
 
   picojson::value json_result = picojson::value(picojson::object());
 
-  ret = IotconUtils::RemoteResourceToJson(resource, &(json_result.get<picojson::object>()));
+  auto ret = IotconUtils::RemoteResourceToJson(resource, &(json_result.get<picojson::object>()));
   if (!ret) {
     data->fun(ret, picojson::value{});
     return IOTCON_FUNC_STOP;
@@ -1237,20 +1243,6 @@ common::TizenResult IotconInstance::ClientFindResource(const picojson::object& a
   if (!result) {
     delete data;
     LogAndReturnTizenError(result);
-  } else {
-    int timeout = 60; //default value set much bigger than default value for iotcon = 30s
-    auto result = IotconUtils::ConvertIotconError(iotcon_get_timeout(&timeout));
-    if (!result) {
-      LoggerE("iotcon_get_timeout - function call failed, using default value %d", timeout);
-    } else {
-      timeout = timeout + 1; //add one extra second to prevent too fast delete
-    }
-    // adding listener to delete data, when find would be finished
-    std::thread([data, timeout]() {
-      std::this_thread::sleep_for(std::chrono::seconds(timeout));
-      LoggerD("Deleting resource find data: %p", data);
-      delete data;
-    }).detach();
   }
 
   return common::TizenSuccess();
@@ -1333,14 +1325,18 @@ bool IotconDeviceInfoCb(iotcon_device_info_h device_info,
   picojson::value v{picojson::object{}};
   common::TizenResult ret = common::TizenSuccess();
 
-  if (IOTCON_ERROR_NONE != result) {
-    ret = IotconUtils::ConvertIotconError(result);
-  } else {
-    ret = IotconUtils::DeviceInfoToJson(device_info,&v.get<picojson::object>());
+  switch (result) {
+    case IOTCON_ERROR_NONE:
+      ret = IotconUtils::DeviceInfoToJson(device_info,&v.get<picojson::object>());
+      break;
+    case IOTCON_ERROR_TIMEOUT:
+      delete data;
+      return IOTCON_FUNC_STOP;
+    default:
+      ret = IotconUtils::ConvertIotconError(result);
   }
 
   data->fun(ret, v);
-  delete data;
 
   return IOTCON_FUNC_CONTINUE;
 }
@@ -1404,14 +1400,18 @@ bool IotconPlatformInfoCb(iotcon_platform_info_h platform_info,
   picojson::value v{picojson::object{}};
   common::TizenResult ret = common::TizenSuccess();
 
-  if (IOTCON_ERROR_NONE != result) {
-    ret = IotconUtils::ConvertIotconError(result);
-  } else {
-    ret = IotconUtils::PlatformInfoToJson(platform_info,&v.get<picojson::object>());
+  switch (result) {
+    case IOTCON_ERROR_NONE:
+      ret = IotconUtils::PlatformInfoToJson(platform_info,&v.get<picojson::object>());
+      break;
+    case IOTCON_ERROR_TIMEOUT:
+      delete data;
+      return IOTCON_FUNC_STOP;
+    default:
+      ret = IotconUtils::ConvertIotconError(result);
   }
 
   data->fun(ret, v);
-  delete data;
 
   return IOTCON_FUNC_CONTINUE;
 }