Fix resource leaks in qt-ble.cpp

1) Destroy QLowEnergyService objects in destructor of BLEObject.

2) Let BLE object take ownership of the controller so that the
latter can be destroyed in the destructor of the former. This
introduces a certain ownership subtlety, which could be solved by
allocating the controller object in the BLE object. But let's
first do the less intrusive thing.

3) Destroy the BLE object for two error conditions.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2017-11-01 15:25:24 +01:00 committed by Lubomir I. Ivanov
parent d23bd46a1b
commit 28299b0ebc

View file

@ -140,6 +140,11 @@ BLEObject::BLEObject(QLowEnergyController *c, dc_user_device_t *d)
BLEObject::~BLEObject() BLEObject::~BLEObject()
{ {
qDebug() << "Deleting BLE object"; qDebug() << "Deleting BLE object";
foreach (QLowEnergyService *service, services)
delete service;
delete controller;
} }
dc_status_t BLEObject::write(const void *data, size_t size, size_t *actual) dc_status_t BLEObject::write(const void *data, size_t size, size_t *actual)
@ -329,7 +334,9 @@ dc_status_t qt_ble_open(dc_custom_io_t *io, dc_context_t *context, const char *d
return DC_STATUS_IO; return DC_STATUS_IO;
} }
/* We need to discover services etc here! */ // We need to discover services etc here!
// Note that ble takes ownership of controller and henceforth deleting ble will
// take care of deleting controller.
BLEObject *ble = new BLEObject(controller, io->user_device); BLEObject *ble = new BLEObject(controller, io->user_device);
ble->connect(controller, SIGNAL(serviceDiscovered(QBluetoothUuid)), SLOT(addService(QBluetoothUuid))); ble->connect(controller, SIGNAL(serviceDiscovered(QBluetoothUuid)), SLOT(addService(QBluetoothUuid)));
@ -348,7 +355,7 @@ dc_status_t qt_ble_open(dc_custom_io_t *io, dc_context_t *context, const char *d
qDebug() << "failed to find suitable service on" << devaddr; qDebug() << "failed to find suitable service on" << devaddr;
report_error("Failed to find suitable service on '%s'", devaddr); report_error("Failed to find suitable service on '%s'", devaddr);
controller->disconnectFromDevice(); controller->disconnectFromDevice();
delete controller; delete ble;
return DC_STATUS_IO; return DC_STATUS_IO;
} }
@ -363,7 +370,7 @@ dc_status_t qt_ble_open(dc_custom_io_t *io, dc_context_t *context, const char *d
qDebug() << "failed to find suitable service on" << devaddr; qDebug() << "failed to find suitable service on" << devaddr;
report_error("Failed to find suitable service on '%s'", devaddr); report_error("Failed to find suitable service on '%s'", devaddr);
controller->disconnectFromDevice(); controller->disconnectFromDevice();
delete controller; delete ble;
return DC_STATUS_IO; return DC_STATUS_IO;
} }
@ -378,8 +385,10 @@ dc_status_t qt_ble_open(dc_custom_io_t *io, dc_context_t *context, const char *d
if (IS_HW(io->user_device)) { if (IS_HW(io->user_device)) {
dc_status_t r = ble->setupHwTerminalIo(list); dc_status_t r = ble->setupHwTerminalIo(list);
if (r != DC_STATUS_SUCCESS) if (r != DC_STATUS_SUCCESS) {
delete ble;
return r; return r;
}
} else { } else {
QList<QLowEnergyDescriptor> l = c.descriptors(); QList<QLowEnergyDescriptor> l = c.descriptors();