[Bluetooth] Fixed handling of allocated data.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Wed, 3 Jun 2015 13:58:03 +0000 (15:58 +0200)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Wed, 3 Jun 2015 13:58:03 +0000 (15:58 +0200)
Prevent CID: 386129

[Verification] Code compiles.

Change-Id: I89c39a636d4fc0b0299387441fe0889d81fc8f84
Signed-off-by: Pawel Andruszkiewicz <p.andruszkie@samsung.com>
src/bluetooth/bluetooth_gatt_service.cc

index 5f79f74..53c2594 100755 (executable)
@@ -397,11 +397,10 @@ void BluetoothGATTService::WriteValue(const picojson::value& args,
     BluetoothGATTService* service;
   };
 
-  Data* user_data = new Data{callback_handle, this};
   bt_gatt_h handle = (bt_gatt_h) static_cast<long>(args.get("handle").get<double>());
 
   auto write_value = [](int result, bt_gatt_h handle, void *user_data) -> void {
-    Data* data = (Data*) user_data;
+    Data* data = static_cast<Data*>(user_data);
     double callback_handle = data->callback_handle;
     BluetoothGATTService* service = data->service;
     delete data;
@@ -424,10 +423,10 @@ void BluetoothGATTService::WriteValue(const picojson::value& args,
     }, response);
   };
 
-
   int ret = bt_gatt_set_value(handle, value_data.get(), value_size);
+
   if (BT_ERROR_NONE != ret) {
-    LOGE("Couldn't set value");
+    LoggerE("Couldn't set value");
     std::shared_ptr<picojson::value> response =
         std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
     ReportError(util::GetBluetoothError(ret, "Failed to set value"),
@@ -437,9 +436,11 @@ void BluetoothGATTService::WriteValue(const picojson::value& args,
       instance_.SyncResponse(callback_handle, response);
     }, response);
   } else {
-    ret = bt_gatt_client_write_value(handle, write_value, (void*)user_data);
+    Data* user_data = new Data{callback_handle, this};
+    ret = bt_gatt_client_write_value(handle, write_value, user_data);
     if (BT_ERROR_NONE != ret) {
-      LOGE("Couldn't register callback for write value");
+      delete user_data;
+      LoggerE("Couldn't register callback for write value");
     }
   }
   ReportSuccess(out);