From 6e2d89afd473a53a3ce6b42b1b3d776361f2e498 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 30 Aug 2017 07:50:24 -0400 Subject: [PATCH] Don't leak memory when winnowing disk devices - Improve logging a little - Don't leak Device*, but delete the raw pointer when erasing - Document that DeviceInfo takes ownership and doesn't leak --- src/modules/partition/core/DeviceList.cpp | 24 +++++++++++++------ .../partition/core/PartitionCoreModule.cpp | 1 + 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/modules/partition/core/DeviceList.cpp b/src/modules/partition/core/DeviceList.cpp index 5bce4a0e3..055c18dbc 100644 --- a/src/modules/partition/core/DeviceList.cpp +++ b/src/modules/partition/core/DeviceList.cpp @@ -106,12 +106,22 @@ operator <<( QDebug& s, QList< Device* >::iterator& it ) return s; } +using DeviceList = QList< Device* >; + +static inline DeviceList::iterator +erase(DeviceList& l, DeviceList::iterator& it) +{ + Device* p = *it; + auto r = l.erase( it ); + if (p) + delete p; + return r; +} + QList< Device* > getDevices( DeviceType which, qint64 minimumSize ) { bool writableOnly = (which == DeviceType::WritableOnly); - using DeviceList = QList< Device* >; - CoreBackend* backend = CoreBackendManager::self()->backend(); DeviceList devices = backend->scanDevices( true ); @@ -123,8 +133,8 @@ QList< Device* > getDevices( DeviceType which, qint64 minimumSize ) ( *it )->deviceNode().startsWith( "/dev/zram" ) ) { - cDebug() << " .. Removing" << it; - it = devices.erase( it ); + cDebug() << " .. Removing zram" << it; + it = erase(devices, it ); } else if ( writableOnly && ( @@ -132,13 +142,13 @@ QList< Device* > getDevices( DeviceType which, qint64 minimumSize ) isIso9660( *it ) ) ) { - cDebug() << " .. Removing" << it; - it = devices.erase( it ); + cDebug() << " .. Removing root-or-CD" << it; + it = erase(devices, it ); } else if ( (minimumSize >= 0) && !( (*it)->capacity() > minimumSize ) ) { cDebug() << " .. Removing too-small" << it; - it = devices.erase( it ); + it = erase(devices, it ); } else ++it; diff --git a/src/modules/partition/core/PartitionCoreModule.cpp b/src/modules/partition/core/PartitionCoreModule.cpp index ac22b2b7a..07c09c6d7 100644 --- a/src/modules/partition/core/PartitionCoreModule.cpp +++ b/src/modules/partition/core/PartitionCoreModule.cpp @@ -122,6 +122,7 @@ PartitionCoreModule::doInit() cDebug() << "node\tcapacity\tname\tprettyName"; for ( auto device : devices ) { + // Gives ownership of the Device* to the DeviceInfo object auto deviceInfo = new DeviceInfo( device ); m_deviceInfos << deviceInfo; cDebug() << device->deviceNode() << device->capacity() << device->name() << device->prettyName();