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
This commit is contained in:
Adriaan de Groot 2017-08-30 07:50:24 -04:00 committed by Philip
parent 4f34c68a14
commit 6e2d89afd4
2 changed files with 18 additions and 7 deletions

View File

@ -106,12 +106,22 @@ operator <<( QDebug& s, QList< Device* >::iterator& it )
return s; 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 ) QList< Device* > getDevices( DeviceType which, qint64 minimumSize )
{ {
bool writableOnly = (which == DeviceType::WritableOnly); bool writableOnly = (which == DeviceType::WritableOnly);
using DeviceList = QList< Device* >;
CoreBackend* backend = CoreBackendManager::self()->backend(); CoreBackend* backend = CoreBackendManager::self()->backend();
DeviceList devices = backend->scanDevices( true ); DeviceList devices = backend->scanDevices( true );
@ -123,8 +133,8 @@ QList< Device* > getDevices( DeviceType which, qint64 minimumSize )
( *it )->deviceNode().startsWith( "/dev/zram" ) ( *it )->deviceNode().startsWith( "/dev/zram" )
) )
{ {
cDebug() << " .. Removing" << it; cDebug() << " .. Removing zram" << it;
it = devices.erase( it ); it = erase(devices, it );
} }
else if ( writableOnly && ( else if ( writableOnly && (
@ -132,13 +142,13 @@ QList< Device* > getDevices( DeviceType which, qint64 minimumSize )
isIso9660( *it ) ) isIso9660( *it ) )
) )
{ {
cDebug() << " .. Removing" << it; cDebug() << " .. Removing root-or-CD" << it;
it = devices.erase( it ); it = erase(devices, it );
} }
else if ( (minimumSize >= 0) && !( (*it)->capacity() > minimumSize ) ) else if ( (minimumSize >= 0) && !( (*it)->capacity() > minimumSize ) )
{ {
cDebug() << " .. Removing too-small" << it; cDebug() << " .. Removing too-small" << it;
it = devices.erase( it ); it = erase(devices, it );
} }
else else
++it; ++it;

View File

@ -122,6 +122,7 @@ PartitionCoreModule::doInit()
cDebug() << "node\tcapacity\tname\tprettyName"; cDebug() << "node\tcapacity\tname\tprettyName";
for ( auto device : devices ) for ( auto device : devices )
{ {
// Gives ownership of the Device* to the DeviceInfo object
auto deviceInfo = new DeviceInfo( device ); auto deviceInfo = new DeviceInfo( device );
m_deviceInfos << deviceInfo; m_deviceInfos << deviceInfo;
cDebug() << device->deviceNode() << device->capacity() << device->name() << device->prettyName(); cDebug() << device->deviceNode() << device->capacity() << device->name() << device->prettyName();