diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2017-11-01 15:25:24 +0100 |
---|---|---|
committer | Lubomir I. Ivanov <neolit123@gmail.com> | 2017-11-02 15:13:58 +0100 |
commit | 28299b0ebcb47c43a2efcaac9dcb1c2a59d41323 (patch) | |
tree | a143108fe3543bac30d84d31d985bd93fe6b89ce /core/qt-ble.cpp | |
parent | d23bd46a1be2dfb25293639abcf06b5b4d4b94df (diff) | |
download | subsurface-28299b0ebcb47c43a2efcaac9dcb1c2a59d41323.tar.gz |
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>
Diffstat (limited to 'core/qt-ble.cpp')
-rw-r--r-- | core/qt-ble.cpp | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/core/qt-ble.cpp b/core/qt-ble.cpp index a72736d4f..678296b29 100644 --- a/core/qt-ble.cpp +++ b/core/qt-ble.cpp @@ -140,6 +140,11 @@ BLEObject::BLEObject(QLowEnergyController *c, dc_user_device_t *d) BLEObject::~BLEObject() { 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) @@ -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; } - /* 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); 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; report_error("Failed to find suitable service on '%s'", devaddr); controller->disconnectFromDevice(); - delete controller; + delete ble; 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; report_error("Failed to find suitable service on '%s'", devaddr); controller->disconnectFromDevice(); - delete controller; + delete ble; 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)) { dc_status_t r = ble->setupHwTerminalIo(list); - if (r != DC_STATUS_SUCCESS) + if (r != DC_STATUS_SUCCESS) { + delete ble; return r; + } } else { QList<QLowEnergyDescriptor> l = c.descriptors(); |