Use std::unique_ptr for memory management in QMimeDataBasePrivate

This change simplifies memory management as long as it doesn't requires
manuall calls to qDeleteAll and manual deleting/nulling pointers.

Change-Id: Id0dc563d0b1e74ae33939d90d4b39999020cd7ce
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
This commit is contained in:
Ivan Komissarov 2018-06-29 16:31:31 +03:00
parent 6c543879a3
commit 17f4adfe18
2 changed files with 16 additions and 15 deletions

View File

@ -76,7 +76,6 @@ QMimeDatabasePrivate::QMimeDatabasePrivate()
QMimeDatabasePrivate::~QMimeDatabasePrivate()
{
qDeleteAll(m_providers);
}
#ifdef QT_BUILD_INTERNAL
@ -116,37 +115,37 @@ void QMimeDatabasePrivate::loadProviders()
const QString cacheFile = mimeDir + QStringLiteral("/mime.cache");
QFileInfo fileInfo(cacheFile);
// Check if we already have a provider for this dir
const auto it = std::find_if(currentProviders.begin(), currentProviders.end(), [mimeDir](QMimeProviderBase *prov) { return prov->directory() == mimeDir; });
const auto predicate = [mimeDir](const std::unique_ptr<QMimeProviderBase> &prov)
{
return prov && prov->directory() == mimeDir;
};
const auto it = std::find_if(currentProviders.begin(), currentProviders.end(), predicate);
if (it == currentProviders.end()) {
QMimeProviderBase *provider = nullptr;
std::unique_ptr<QMimeProviderBase> provider;
#if defined(QT_USE_MMAP)
if (qEnvironmentVariableIsEmpty("QT_NO_MIME_CACHE") && fileInfo.exists()) {
provider = new QMimeBinaryProvider(this, mimeDir);
provider.reset(new QMimeBinaryProvider(this, mimeDir));
//qDebug() << "Created binary provider for" << mimeDir;
if (!provider->isValid()) {
delete provider;
provider = nullptr;
provider.reset();
}
}
#endif
if (!provider) {
provider = new QMimeXMLProvider(this, mimeDir);
provider.reset(new QMimeXMLProvider(this, mimeDir));
//qDebug() << "Created XML provider for" << mimeDir;
}
m_providers.push_back(provider);
m_providers.push_back(std::move(provider));
} else {
QMimeProviderBase *provider = *it;
currentProviders.erase(it);
auto provider = std::move(*it); // take provider out of the vector
provider->ensureLoaded();
if (!provider->isValid()) {
delete provider;
provider = new QMimeXMLProvider(this, mimeDir);
provider.reset(new QMimeXMLProvider(this, mimeDir));
//qDebug() << "Created XML provider to replace binary provider for" << mimeDir;
}
m_providers.push_back(provider);
m_providers.push_back(std::move(provider));
}
}
qDeleteAll(currentProviders);
}
const QMimeDatabasePrivate::Providers &QMimeDatabasePrivate::providers()

View File

@ -63,6 +63,8 @@
#include <QtCore/qmutex.h>
#include <QtCore/qvector.h>
#include <memory>
QT_BEGIN_NAMESPACE
class QIODevice;
@ -102,7 +104,7 @@ public:
bool mimeInherits(const QString &mime, const QString &parent);
private:
using Providers = std::vector<QMimeProviderBase *>;
using Providers = std::vector<std::unique_ptr<QMimeProviderBase>>;
const Providers &providers();
bool shouldCheck();
void loadProviders();