Fix memory leaks

This commit is contained in:
Jonas Kvinge
2019-07-22 20:53:05 +02:00
parent 2df21081a1
commit bd78e8c275
33 changed files with 186 additions and 74 deletions

View File

@@ -210,6 +210,7 @@ int Database::FTSNext(sqlite3_tokenizer_cursor *cursor, const char* *token, int
void Database::StaticInit() {
if (sFTSTokenizer) return;
sFTSTokenizer = new sqlite3_tokenizer_module;
sFTSTokenizer->iVersion = 0;
sFTSTokenizer->xCreate = &Database::FTSCreate;
@@ -241,6 +242,8 @@ Database::Database(Application *app, QObject *parent, const QString &database_na
}
Database::~Database() {}
QSqlDatabase Database::Connect() {
QMutexLocker l(&connect_mutex_);
@@ -273,7 +276,7 @@ QSqlDatabase Database::Connect() {
}
// Find Sqlite3 functions in the Qt plugin.
StaticInit();
if (!sFTSTokenizer) StaticInit();
{

View File

@@ -51,6 +51,7 @@ class Database : public QObject {
public:
Database(Application *app, QObject *parent = nullptr, const QString &database_name = QString());
~Database();
struct AttachedDatabase {
AttachedDatabase() {}

View File

@@ -1799,7 +1799,7 @@ void MainWindow::SongSaveComplete(TagReaderReply *reply, const QPersistentModelI
if (reply->is_successful() && index.isValid()) {
app_->playlist_manager()->current()->ReloadItems(QList<int>()<< index.row());
}
metaObject()->invokeMethod(reply, "deleteLater", Qt::QueuedConnection);
reply->deleteLater();
}

View File

@@ -39,12 +39,15 @@
#include "network.h"
QMutex ThreadSafeNetworkDiskCache::sMutex;
ThreadSafeNetworkDiskCache *ThreadSafeNetworkDiskCache::sInstance = nullptr;
QNetworkDiskCache *ThreadSafeNetworkDiskCache::sCache = nullptr;
ThreadSafeNetworkDiskCache::ThreadSafeNetworkDiskCache(QObject *parent)
: QAbstractNetworkCache(parent) {
QMutexLocker l(&sMutex);
if (!sCache) {
sInstance = this;
sCache = new QNetworkDiskCache;
#ifdef Q_OS_WIN32
sCache->setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/strawberry/networkcache");
@@ -52,6 +55,11 @@ ThreadSafeNetworkDiskCache::ThreadSafeNetworkDiskCache(QObject *parent)
sCache->setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/networkcache");
#endif
}
}
ThreadSafeNetworkDiskCache::~ThreadSafeNetworkDiskCache() {
if (this == sInstance) delete sCache;
}
qint64 ThreadSafeNetworkDiskCache::cacheSize() const {

View File

@@ -47,6 +47,7 @@ class QTimerEvent;
class ThreadSafeNetworkDiskCache : public QAbstractNetworkCache {
public:
explicit ThreadSafeNetworkDiskCache(QObject *parent);
~ThreadSafeNetworkDiskCache();
qint64 cacheSize() const;
QIODevice *data(const QUrl &url);
@@ -60,6 +61,7 @@ class ThreadSafeNetworkDiskCache : public QAbstractNetworkCache {
private:
static QMutex sMutex;
static ThreadSafeNetworkDiskCache *sInstance;
static QNetworkDiskCache *sCache;
};

View File

@@ -20,8 +20,11 @@
#include "config.h"
#include <QFileSystemWatcher>
#include <QString>
#include "core/logging.h"
#include "filesystemwatcherinterface.h"
#include "qtfslistener.h"
@@ -33,9 +36,7 @@ QtFSListener::QtFSListener(QObject *parent) : FileSystemWatcherInterface(parent)
void QtFSListener::AddPath(const QString &path) { watcher_.addPath(path); }
void QtFSListener::RemovePath(const QString &path) {
watcher_.removePath(path);
}
void QtFSListener::RemovePath(const QString &path) { watcher_.removePath(path); }
void QtFSListener::Clear() {
watcher_.removePaths(watcher_.directories());

50
src/core/translations.cpp Normal file
View File

@@ -0,0 +1,50 @@
/*
* Strawberry Music Player
* Copyright 2019, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include <QCoreApplication>
#include <QTranslator>
#include <QString>
#include "translations.h"
#include "core/potranslator.h"
Translations::Translations() {}
Translations::~Translations() {
for (QTranslator *t : translations_) {
QCoreApplication::removeTranslator(t);
delete t;
}
}
void Translations::LoadTranslation(const QString &prefix, const QString &path, const QString &language) {
QTranslator *t = new PoTranslator;
if (t->load(prefix + "_" + language, path)) {
QCoreApplication::installTranslator(t);
translations_ << t;
}
else {
delete t;
}
}

36
src/core/translations.h Normal file
View File

@@ -0,0 +1,36 @@
/*
* Strawberry Music Player
* Copyright 2019, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include <QObject>
#include <QTranslator>
#include <QList>
#include <QString>
class Translations : public QObject {
public:
Translations();
~Translations();
void LoadTranslation(const QString &prefix, const QString &path, const QString &language);
private:
QList<QTranslator*> translations_;
};

View File

@@ -60,9 +60,6 @@
#include <QtEvents>
#include <QMessageBox>
#include <QtDebug>
#ifdef HAVE_TRANSLATIONS
# include <QTranslator>
#endif
#ifdef Q_OS_LINUX
# include <unistd.h>
@@ -101,10 +98,6 @@
# include "scoped_cftyperef.h"
#endif
#ifdef HAVE_TRANSLATIONS
# include "potranslator.h"
#endif
namespace Utilities {
static QString tr(const char *str) {
@@ -821,29 +814,6 @@ QString UnicodeToAscii(const QString &unicode) {
}
#ifdef HAVE_TRANSLATIONS
QString SystemLanguageName() {
QString system_language = QLocale::system().uiLanguages().empty() ? QLocale::system().name() : QLocale::system().uiLanguages().first();
// uiLanguages returns strings with "-" as separators for language/region; however QTranslator needs "_" separators
system_language.replace("-", "_");
return system_language;
}
void LoadTranslation(const QString &prefix, const QString &path, const QString &language) {
QTranslator *t = new PoTranslator;
if (t->load(prefix + "_" + language, path))
QCoreApplication::installTranslator(t);
else
delete t;
}
#endif
} // namespace Utilities
ScopedWCharArray::ScopedWCharArray(const QString &str)

View File

@@ -157,11 +157,6 @@ QString DesktopEnvironment();
QString UnicodeToAscii(const QString &unicode);
#ifdef HAVE_TRANSLATIONS
QString SystemLanguageName();
void LoadTranslation(const QString &prefix, const QString &path, const QString &language);
#endif
} // namespace
class ScopedWCharArray {