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

@ -112,6 +112,7 @@ set(SOURCES
core/windows7thumbbar.cpp
core/screensaver.cpp
core/scopedtransaction.cpp
core/translations.cpp
engine/enginetype.cpp
engine/enginebase.cpp

View File

@ -68,6 +68,7 @@ SCollection::~SCollection() {
watcher_->deleteLater();
watcher_thread_->exit();
watcher_thread_->wait(5000 /* five seconds */);
backend_->deleteLater();
}
void SCollection::Init() {

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 {

View File

@ -31,6 +31,14 @@
CoverProviders::CoverProviders(QObject *parent) : QObject(parent) {}
CoverProviders::~CoverProviders() {
while (!cover_providers_.isEmpty()) {
delete cover_providers_.firstKey();
}
}
void CoverProviders::AddProvider(CoverProvider *provider) {
{

View File

@ -42,6 +42,7 @@ class CoverProviders : public QObject {
public:
explicit CoverProviders(QObject *parent = nullptr);
~CoverProviders();
// Lets a cover provider register itself in the repository.
void AddProvider(CoverProvider *provider);

View File

@ -46,6 +46,7 @@ DeviceLister::~DeviceLister() {
if (thread_) {
thread_->quit();
thread_->wait(1000);
thread_->deleteLater();
}
}

View File

@ -910,6 +910,6 @@ void EditTagDialog::SongSaveComplete(TagReaderReply *reply, const QString &filen
if (pending_ <= 0) AcceptFinished();
metaObject()->invokeMethod(reply, "deleteLater", Qt::QueuedConnection);
reply->deleteLater();
}

View File

@ -43,6 +43,7 @@ GstStartup::GstStartup(QObject *parent) : QObject(parent) {
}
GstStartup::~GstStartup() {
//gst_deinit();
}
void GstStartup::InitialiseGStreamer() {

View File

@ -31,12 +31,18 @@
#include "internetservice.h"
InternetServices::InternetServices(QObject *parent) : QObject(parent) {}
InternetServices::~InternetServices() {}
InternetServices::~InternetServices() {
while (!services_.isEmpty()) {
delete services_.take(services_.firstKey());
}
}
void InternetServices::AddService(InternetService *service) {
services_.insert(service->source(), service);
connect(service, SIGNAL(destroyed()), SLOT(ServiceDeleted()));
if (service->has_initial_load_settings()) service->InitialLoadSettings();
else service->ReloadSettings();
@ -50,12 +56,7 @@ void InternetServices::RemoveService(InternetService *service) {
services_.remove(service->source());
disconnect(service, 0, this, 0);
}
void InternetServices::ServiceDeleted() {
InternetService *service = qobject_cast<InternetService*>(sender());
if (service) RemoveService(service);
qLog(Debug) << "Removed internet service" << service->name();
}

View File

@ -48,9 +48,6 @@ class InternetServices : public QObject {
void RemoveService(InternetService *service);
void ReloadSettings();
private slots:
void ServiceDeleted();
private:
QMap<Song::Source, InternetService*> services_;

View File

@ -32,6 +32,14 @@
LyricsProviders::LyricsProviders(QObject *parent) : QObject(parent) {}
LyricsProviders::~LyricsProviders() {
while (!lyrics_providers_.isEmpty()) {
delete lyrics_providers_.firstKey();
}
}
void LyricsProviders::AddProvider(LyricsProvider *provider) {
{

View File

@ -39,6 +39,8 @@ class LyricsProviders : public QObject {
public:
explicit LyricsProviders(QObject *parent = nullptr);
~LyricsProviders();
void AddProvider(LyricsProvider *provider);
void RemoveProvider(LyricsProvider *provider);
QList<LyricsProvider*> List() const { return lyrics_providers_.keys(); }

View File

@ -90,7 +90,7 @@
#include "core/networkproxyfactory.h"
#include "core/scangiomodulepath.h"
#ifdef HAVE_TRANSLATIONS
# include "core/potranslator.h"
# include "core/translations.h"
#endif
#include "settings/behavioursettingspage.h"
@ -228,12 +228,18 @@ int main(int argc, char* argv[]) {
s.endGroup();
}
const QString language = override_language.isEmpty() ? Utilities::SystemLanguageName() : override_language;
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("-", "_");
Utilities::LoadTranslation("qt", QLibraryInfo::location(QLibraryInfo::TranslationsPath), language);
Utilities::LoadTranslation("strawberry", ":/translations", language);
Utilities::LoadTranslation("strawberry", a.applicationDirPath(), language);
Utilities::LoadTranslation("strawberry", QDir::currentPath(), language);
const QString language = override_language.isEmpty() ? system_language : override_language;
std::unique_ptr<Translations> translations(new Translations);
translations->LoadTranslation("qt", QLibraryInfo::location(QLibraryInfo::TranslationsPath), language);
translations->LoadTranslation("strawberry", ":/translations", language);
translations->LoadTranslation("strawberry", a.applicationDirPath(), language);
translations->LoadTranslation("strawberry", QDir::currentPath(), language);
#endif
Application app;

View File

@ -24,6 +24,7 @@
#include <QStyleOptionComplex>
#include <QStyleOptionSlider>
#include <QTimeLine>
#include <QStyle>
#include <QEvent>
#include <QContextMenuEvent>
@ -59,8 +60,11 @@ MoodbarProxyStyle::MoodbarProxyStyle(Application* app, QSlider* slider)
connect(app, SIGNAL(SettingsChanged()), SLOT(ReloadSettings()));
ReloadSettings();
}
MoodbarProxyStyle::~MoodbarProxyStyle() {}
void MoodbarProxyStyle::ReloadSettings() {
QSettings s;

View File

@ -38,6 +38,7 @@ class MoodbarProxyStyle : public QProxyStyle {
public:
MoodbarProxyStyle(Application* app, QSlider* slider);
~MoodbarProxyStyle();
// QProxyStyle
void drawComplexControl(ComplexControl control, const QStyleOptionComplex* option, QPainter* painter, const QWidget* widget) const;

View File

@ -399,8 +399,7 @@ void Playlist::SongSaveComplete(TagReaderReply *reply, const QPersistentModelInd
emit Error(tr("An error occurred writing metadata to '%1'").arg(QString::fromStdString(reply->request_message().save_file_request().filename())));
}
}
metaObject()->invokeMethod(reply, "deleteLater", Qt::QueuedConnection);
reply->deleteLater();
}

View File

@ -395,12 +395,17 @@ TagCompleter::TagCompleter(CollectionBackend *backend, Playlist::Column column,
}
TagCompleter::~TagCompleter() {
delete model();
}
void TagCompleter::ModelReady(QFuture<TagCompletionModel*> future) {
TagCompletionModel *model = future.result();
setModel(model);
setCaseSensitivity(Qt::CaseInsensitive);
editor_->setCompleter(this);
}
QWidget *TagCompletionItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem&, const QModelIndex&) const {
@ -409,6 +414,7 @@ QWidget *TagCompletionItemDelegate::createEditor(QWidget *parent, const QStyleOp
new TagCompleter(backend_, column_, editor);
return editor;
}
QString NativeSeparatorsDelegate::displayText(const QVariant &value, const QLocale&) const {

View File

@ -144,6 +144,7 @@ class TagCompleter : public QCompleter {
public:
TagCompleter(CollectionBackend *backend, Playlist::Column column, QLineEdit *editor);
~TagCompleter();
private slots:
void ModelReady(QFuture<TagCompletionModel*> future);

View File

@ -180,6 +180,10 @@ QobuzService::~QobuzService() {
stream_url_req->deleteLater();
}
artists_collection_backend_->deleteLater();
albums_collection_backend_->deleteLater();
songs_collection_backend_->deleteLater();
}
void QobuzService::ShowConfig() {

View File

@ -32,14 +32,19 @@
ScrobblerServices::ScrobblerServices(QObject *parent) : QObject(parent) {}
ScrobblerServices::~ScrobblerServices() {}
ScrobblerServices::~ScrobblerServices() {
while (!scrobbler_services_.isEmpty()) {
delete scrobbler_services_.take(scrobbler_services_.firstKey());
}
}
void ScrobblerServices::AddService(ScrobblerService *service) {
{
QMutexLocker locker(&mutex_);
scrobbler_services_.insert(service->name(), service);
connect(service, SIGNAL(destroyed()), SLOT(ServiceDestroyed()));
}
qLog(Debug) << "Registered scrobbler service" << service->name();
@ -60,13 +65,6 @@ void ScrobblerServices::RemoveService(ScrobblerService *service) {
}
void ScrobblerServices::ServiceDestroyed() {
ScrobblerService *service = static_cast<ScrobblerService*>(sender());
RemoveService(service);
}
int ScrobblerServices::NextId() { return next_id_.fetchAndAddRelaxed(1); }
ScrobblerService *ScrobblerServices::ServiceByName(const QString &name) {

View File

@ -53,9 +53,6 @@ class ScrobblerServices : public QObject {
return static_cast<T*>(this->ServiceByName(T::kName));
}
private slots:
void ServiceDestroyed();
private:
Q_DISABLE_COPY(ScrobblerServices);

View File

@ -94,7 +94,9 @@ SubsonicService::SubsonicService(Application *app, QObject *parent)
}
SubsonicService::~SubsonicService() {}
SubsonicService::~SubsonicService() {
collection_backend_->deleteLater();
}
void SubsonicService::ShowConfig() {
app_->OpenSettingsDialogAtPage(SettingsDialog::Page_Subsonic);

View File

@ -186,6 +186,10 @@ TidalService::~TidalService() {
stream_url_req->deleteLater();
}
artists_collection_backend_->deleteLater();
albums_collection_backend_->deleteLater();
songs_collection_backend_->deleteLater();
}
void TidalService::ShowConfig() {

View File

@ -72,11 +72,14 @@ TrackSlider::TrackSlider(QWidget* parent)
TrackSlider::~TrackSlider() {
delete ui_;
#ifdef HAVE_MOODBAR
if (moodbar_style_) moodbar_style_->deleteLater();
#endif
}
void TrackSlider::SetApplication(Application* app) {
#ifdef HAVE_MOODBAR
moodbar_style_ = new MoodbarProxyStyle(app, ui_->slider);
if (!moodbar_style_) moodbar_style_ = new MoodbarProxyStyle(app, ui_->slider);
#endif
}