[Bluetooth] Bugfix: prevent memory leak 81/244481/2
authorPawel Wasowski <p.wasowski2@samsung.com>
Wed, 16 Sep 2020 14:11:26 +0000 (16:11 +0200)
committerPiotr Kosko/Native/Web API (PLT) /SRPOL/Engineer/Samsung Electronics <p.kosko@samsung.com>
Mon, 21 Sep 2020 07:56:05 +0000 (09:56 +0200)
In the previous implementation if a service was successfully created,
but its registration failed, the resources associated with its handle
were not released.
Such a situation has occurred when Bluetooth was disabled and
BluetoothGATTServer::registerService() was called.
This commit adds the missing line that destroys the handle.

[Verification] The code compiles.
Tested in chrome console - server is successfully created and reports
errors in case of disabled bluetooth adapter. No crash or other issues observed.

Change-Id: I85741e51638abd6b5443d7f7ce5d84e9279fc498
Signed-off-by: Pawel Wasowski <p.wasowski2@samsung.com>
src/bluetooth/bluetooth_gatt_server.cc

index e8bdf42..e67d9b3 100644 (file)
@@ -172,6 +172,7 @@ void BluetoothGATTServer::RegisterService(const picojson::value& args, picojson:
      */
     auto result = InitializeAndCreateHandle();
     if (!result) {
+      BluetoothGATTServerService::DestroyService(0, 0, service_handle, nullptr);
       result.SetMessage("Couldn't register the service");
       ReportError(result, &out);
       return;
@@ -180,6 +181,7 @@ void BluetoothGATTServer::RegisterService(const picojson::value& args, picojson:
 
   result = service_.RegisterService(service_handle, this->handle_, new_gatt_objects);
   if (result.IsError()) {
+    BluetoothGATTServerService::DestroyService(0, 0, service_handle, nullptr);
     ReportError(result, &out);
     return;
   }