From c204750493cfed551a9344ad84e5cf006046b516 Mon Sep 17 00:00:00 2001
From: Pawel Wasowski
Date: Wed, 16 Sep 2020 16:11:26 +0200
Subject: [PATCH] [Bluetooth] Bugfix: prevent memory leak
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
---
src/bluetooth/bluetooth_gatt_server.cc | 2 ++
1 file changed, 2 insertions(+)
diff --git a/src/bluetooth/bluetooth_gatt_server.cc b/src/bluetooth/bluetooth_gatt_server.cc
index e8bdf420..e67d9b34 100644
--- a/src/bluetooth/bluetooth_gatt_server.cc
+++ b/src/bluetooth/bluetooth_gatt_server.cc
@@ -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;
}
--
2.34.1