Fix memory leaks.

This commit is contained in:
Andrius Štikonas 2016-06-02 13:46:16 +01:00
parent 09711cdf1d
commit 5e982b5b1e
2 changed files with 26 additions and 13 deletions

View File

@ -143,6 +143,8 @@ static quint64 firstUsableSector(const Device& d)
rval += 32; rval += 32;
} }
ped_disk_destroy(pedDisk);
return rval; return rval;
} }
@ -171,6 +173,8 @@ static quint64 lastUsableSector(const Device& d)
rval -= 32; rval -= 32;
} }
ped_disk_destroy(pedDisk);
return rval; return rval;
} }
@ -321,8 +325,8 @@ void LibPartedBackend::scanDevicePartitions(Device& d, PedDisk* pedDisk)
PartitionRole::Roles r = PartitionRole::None; PartitionRole::Roles r = PartitionRole::None;
char* pedPath = ped_partition_get_path(pedPartition);
FileSystem::Type type = FileSystem::Unknown; FileSystem::Type type = FileSystem::Unknown;
char* pedPath = ped_partition_get_path(pedPartition);
if (pedPath) if (pedPath)
type = detectFileSystem(QString::fromUtf8(pedPath)); type = detectFileSystem(QString::fromUtf8(pedPath));
free(pedPath); free(pedPath);
@ -352,7 +356,9 @@ void LibPartedBackend::scanDevicePartitions(Device& d, PedDisk* pedDisk)
if (parent == nullptr) if (parent == nullptr)
parent = d.partitionTable(); parent = d.partitionTable();
const QString node = QString::fromUtf8(ped_partition_get_path(pedPartition)); pedPath = ped_partition_get_path(pedPartition);
const QString node = QString::fromUtf8(pedPath);
free(pedPath);
FileSystem* fs = FileSystemFactory::create(type, pedPartition->geom.start, pedPartition->geom.end); FileSystem* fs = FileSystemFactory::create(type, pedPartition->geom.start, pedPartition->geom.end);
// libparted does not handle LUKS partitions // libparted does not handle LUKS partitions
@ -445,6 +451,7 @@ Device* LibPartedBackend::scanDevice(const QString& device_node)
scanDevicePartitions(*d, pedDisk); scanDevicePartitions(*d, pedDisk);
} }
ped_device_destroy(pedDevice);
return d; return d;
} }
@ -495,9 +502,9 @@ FileSystem::Type LibPartedBackend::detectFileSystem(const QString& partitionPath
if ((dev = blkid_get_dev(cache, if ((dev = blkid_get_dev(cache,
partitionPath.toLocal8Bit().constData(), partitionPath.toLocal8Bit().constData(),
BLKID_DEV_NORMAL)) != nullptr) { BLKID_DEV_NORMAL)) != nullptr) {
QString s = QString::fromUtf8(blkid_get_tag_value(cache, char *string = blkid_get_tag_value(cache, "TYPE", partitionPath.toLocal8Bit().constData());
"TYPE", QString s = QString::fromUtf8(string);
partitionPath.toLocal8Bit().constData())); free(string);
if (s == QStringLiteral("ext2")) rval = FileSystem::Ext2; if (s == QStringLiteral("ext2")) rval = FileSystem::Ext2;
else if (s == QStringLiteral("ext3")) rval = FileSystem::Ext3; else if (s == QStringLiteral("ext3")) rval = FileSystem::Ext3;
@ -513,9 +520,9 @@ FileSystem::Type LibPartedBackend::detectFileSystem(const QString& partitionPath
else if (s == QStringLiteral("ufs")) rval = FileSystem::Ufs; else if (s == QStringLiteral("ufs")) rval = FileSystem::Ufs;
else if (s == QStringLiteral("vfat")) { else if (s == QStringLiteral("vfat")) {
// libblkid uses SEC_TYPE to distinguish between FAT16 and FAT32 // libblkid uses SEC_TYPE to distinguish between FAT16 and FAT32
QString st = QString::fromUtf8(blkid_get_tag_value(cache, string = blkid_get_tag_value(cache, "SEC_TYPE", partitionPath.toLocal8Bit().constData());
"SEC_TYPE", QString st = QString::fromUtf8(string);
partitionPath.toLocal8Bit().constData())); free(string);
if (st == QStringLiteral("msdos")) if (st == QStringLiteral("msdos"))
rval = FileSystem::Fat16; rval = FileSystem::Fat16;
else else
@ -582,5 +589,4 @@ QString LibPartedBackend::lastPartedExceptionMessage()
return s_lastPartedExceptionMessage; return s_lastPartedExceptionMessage;
} }
#include "libpartedbackend.moc" #include "libpartedbackend.moc"

View File

@ -44,8 +44,7 @@ LibPartedPartitionTable::LibPartedPartitionTable(PedDevice* device) :
LibPartedPartitionTable::~LibPartedPartitionTable() LibPartedPartitionTable::~LibPartedPartitionTable()
{ {
if (m_PedDisk != nullptr) ped_disk_destroy(m_PedDisk);
ped_disk_destroy(m_PedDisk);
} }
bool LibPartedPartitionTable::open() bool LibPartedPartitionTable::open()
@ -167,14 +166,18 @@ QString LibPartedPartitionTable::createPartition(Report& report, const Partition
if (pedGeometry) if (pedGeometry)
pedConstraint = ped_constraint_exact(pedGeometry); pedConstraint = ped_constraint_exact(pedGeometry);
ped_geometry_destroy(pedGeometry);
if (pedConstraint == nullptr) { if (pedConstraint == nullptr) {
report.line() << i18nc("@info/plain", "Failed to create a new partition: could not get geometry for constraint."); report.line() << i18nc("@info/plain", "Failed to create a new partition: could not get geometry for constraint.");
return QString(); return QString();
} }
if (ped_disk_add_partition(pedDisk(), pedPartition, pedConstraint)) if (ped_disk_add_partition(pedDisk(), pedPartition, pedConstraint)) {
rval = QString::fromUtf8(ped_partition_get_path(pedPartition)); char *pedPath = ped_partition_get_path(pedPartition);
rval = QString::fromUtf8(pedPath);
free(pedPath);
}
else { else {
report.line() << xi18nc("@info/plain", "Failed to add partition <filename>%1</filename> to device <filename>%2</filename>.", partition.deviceNode(), QString::fromUtf8(pedDisk()->dev->path)); report.line() << xi18nc("@info/plain", "Failed to add partition <filename>%1</filename> to device <filename>%2</filename>.", partition.deviceNode(), QString::fromUtf8(pedDisk()->dev->path));
report.line() << LibPartedBackend::lastPartedExceptionMessage(); report.line() << LibPartedBackend::lastPartedExceptionMessage();
@ -223,8 +226,10 @@ bool LibPartedPartitionTable::updateGeometry(Report& report, const Partition& pa
rval = true; rval = true;
else else
report.line() << xi18nc("@info/plain", "Could not set geometry for partition <filename>%1</filename> while trying to resize/move it.", partition.deviceNode()); report.line() << xi18nc("@info/plain", "Could not set geometry for partition <filename>%1</filename> while trying to resize/move it.", partition.deviceNode());
ped_constraint_destroy(pedConstraint);
} else } else
report.line() << xi18nc("@info/plain", "Could not get constraint for partition <filename>%1</filename> while trying to resize/move it.", partition.deviceNode()); report.line() << xi18nc("@info/plain", "Could not get constraint for partition <filename>%1</filename> while trying to resize/move it.", partition.deviceNode());
ped_geometry_destroy(pedGeometry);
} else } else
report.line() << xi18nc("@info/plain", "Could not get geometry for partition <filename>%1</filename> while trying to resize/move it.", partition.deviceNode()); report.line() << xi18nc("@info/plain", "Could not get geometry for partition <filename>%1</filename> while trying to resize/move it.", partition.deviceNode());
} else } else
@ -283,12 +288,14 @@ bool LibPartedPartitionTable::resizeFileSystem(Report& report, const Partition&
if (!rval) if (!rval)
report.line() << xi18nc("@info/plain", "Could not resize file system on partition <filename>%1</filename>.", partition.deviceNode()); report.line() << xi18nc("@info/plain", "Could not resize file system on partition <filename>%1</filename>.", partition.deviceNode());
ped_geometry_destroy(resizedGeometry);
} else } else
report.line() << xi18nc("@info/plain", "Could not get geometry for resized partition <filename>%1</filename> while trying to resize the file system.", partition.deviceNode()); report.line() << xi18nc("@info/plain", "Could not get geometry for resized partition <filename>%1</filename> while trying to resize the file system.", partition.deviceNode());
ped_file_system_close(pedFileSystem); ped_file_system_close(pedFileSystem);
} else } else
report.line() << xi18nc("@info/plain", "Could not open partition <filename>%1</filename> while trying to resize the file system.", partition.deviceNode()); report.line() << xi18nc("@info/plain", "Could not open partition <filename>%1</filename> while trying to resize the file system.", partition.deviceNode());
ped_geometry_destroy(originalGeometry);
} else } else
report.line() << xi18nc("@info/plain", "Could not read geometry for partition <filename>%1</filename> while trying to resize the file system.", partition.deviceNode()); report.line() << xi18nc("@info/plain", "Could not read geometry for partition <filename>%1</filename> while trying to resize the file system.", partition.deviceNode());
#else #else