Skip to content
This repository was archived by the owner on Jan 4, 2023. It is now read-only.

Commit b3c053c

Browse files
Tzafrir Poupkopetreeftime
authored andcommitted
Fix memory leak with g_objects
BluetoothObject constructor maintains internally a g_object reference to the underlying object. This means that any g_object obtained externally should be unreferenced after the BluetoothObject is constructed. Signed-off-by: Tzafrir Poupko <[email protected]>
1 parent 6a643b3 commit b3c053c

File tree

6 files changed

+18
-4
lines changed

6 files changed

+18
-4
lines changed

src/BluetoothAdapter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ std::unique_ptr<BluetoothAdapter> BluetoothAdapter::make(Object *object,
139139
(adapter = object_get_adapter1(object)) != NULL) {
140140

141141
std::unique_ptr<BluetoothAdapter> p(new BluetoothAdapter(adapter));
142+
g_object_unref(adapter);
142143

143144
if ((name == nullptr || *name == p->get_name()) &&
144145
(identifier == nullptr || *identifier == p->get_address()) &&

src/BluetoothDevice.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ std::unique_ptr<BluetoothDevice> BluetoothDevice::make(Object *object,
194194
(device = object_get_device1(object)) != NULL) {
195195

196196
std::unique_ptr<BluetoothDevice> p(new BluetoothDevice(device));
197+
g_object_unref(device);
197198

198199
if ((name == nullptr || *name == p->get_name()) &&
199200
(identifier == nullptr || *identifier == p->get_address()) &&
@@ -496,7 +497,9 @@ BluetoothAdapter BluetoothDevice::get_adapter ()
496497
throw BluetoothException(error_msg);
497498
}
498499

499-
return BluetoothAdapter(adapter);
500+
auto res = BluetoothAdapter(adapter);
501+
g_object_unref(adapter);
502+
return res;
500503
}
501504

502505
std::map<uint16_t, std::vector<uint8_t>> BluetoothDevice::get_manufacturer_data()

src/BluetoothGattCharacteristic.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ std::unique_ptr<BluetoothGattCharacteristic> BluetoothGattCharacteristic::make(
109109

110110
std::unique_ptr<BluetoothGattCharacteristic> p(
111111
new BluetoothGattCharacteristic(characteristic));
112+
g_object_unref(characteristic);
112113

113114
if ((name == nullptr) &&
114115
(identifier == nullptr || *identifier == p->get_uuid()) &&
@@ -260,7 +261,9 @@ BluetoothGattService BluetoothGattCharacteristic::get_service ()
260261
throw BluetoothException(error_msg);
261262
}
262263

263-
return BluetoothGattService(service);
264+
auto res = BluetoothGattService(service);
265+
g_object_unref(service);
266+
return res;
264267
}
265268

266269
std::vector<unsigned char> BluetoothGattCharacteristic::get_value ()

src/BluetoothGattDescriptor.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ std::unique_ptr<BluetoothGattDescriptor> BluetoothGattDescriptor::make(
106106

107107
std::unique_ptr<BluetoothGattDescriptor> p(
108108
new BluetoothGattDescriptor(descriptor));
109+
g_object_unref(descriptor);
109110

110111
if ((name == nullptr) &&
111112
(identifier == nullptr || *identifier == p->get_uuid()) &&
@@ -231,7 +232,9 @@ BluetoothGattCharacteristic BluetoothGattDescriptor::get_characteristic ()
231232
throw BluetoothException(error_msg);
232233
}
233234

234-
return BluetoothGattCharacteristic(characteristic);
235+
auto res = BluetoothGattCharacteristic(characteristic);
236+
g_object_unref(characteristic);
237+
return res;
235238
}
236239

237240
std::vector<unsigned char> BluetoothGattDescriptor::get_value ()

src/BluetoothGattService.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ std::unique_ptr<BluetoothGattService> BluetoothGattService::make(
7777

7878
std::unique_ptr<BluetoothGattService> p(
7979
new BluetoothGattService(service));
80+
g_object_unref(service);
8081

8182
if ((name == nullptr) &&
8283
(identifier == nullptr || *identifier == p->get_uuid()) &&
@@ -117,7 +118,9 @@ BluetoothDevice BluetoothGattService::get_device ()
117118
throw BluetoothException(error_msg);
118119
}
119120

120-
return BluetoothDevice(device);
121+
auto res = BluetoothDevice(device);
122+
g_object_unref(device);
123+
return res;
121124
}
122125

123126
bool BluetoothGattService::get_primary ()

src/BluetoothManager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ BluetoothManager::BluetoothManager() : event_list()
287287
Adapter1 *adapter = object_get_adapter1(object);
288288
if (adapter != NULL) {
289289
default_adapter = std::unique_ptr<BluetoothAdapter>(new BluetoothAdapter(adapter));
290+
g_object_unref(adapter);
290291
break;
291292
}
292293
}

0 commit comments

Comments
 (0)