From a7e82bfec78a08a223f6485038b93cb9fc72b4a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Tinkl?= Date: Tue, 6 Dec 2022 13:40:45 +0100 Subject: [PATCH] fix: stop leaking QNetworkAccessManager and its replies this was a memleak and fd leak bomb resulting in the app not starting at all: ``` (nim_status_client:342629): GLib-ERROR **: 11:16:11.873: Creating pipes for GWakeup: Too many open files /home/jakubgs/bin/StatusIm: line 2: 342627 Trace/breakpoint trap (core dumped) appimage-run ~/Downloads/Status/StatusIm-Desktop-221206-082906-5ee51a.AppImage ``` --- vendor/DOtherSide/lib/src/DOtherSide.cpp | 29 ++++++++++++------------ 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/vendor/DOtherSide/lib/src/DOtherSide.cpp b/vendor/DOtherSide/lib/src/DOtherSide.cpp index 39d42068a0..b170246588 100644 --- a/vendor/DOtherSide/lib/src/DOtherSide.cpp +++ b/vendor/DOtherSide/lib/src/DOtherSide.cpp @@ -38,13 +38,11 @@ #include #include #include -#include #include #include #include #include #include -#include #include #include #include @@ -97,7 +95,7 @@ void register_meta_types() } // jrainville: I'm not sure where to put this, but it works like so -QTranslator *m_translator = new QTranslator(); +static QTranslator *m_translator = new QTranslator(); class QMLNetworkAccessFactory : public QQmlNetworkAccessManagerFactory { @@ -110,9 +108,7 @@ class QMLNetworkAccessFactory : public QQmlNetworkAccessManagerFactory } - QNetworkAccessManager* create(QObject* parent); - - void setTmpPath(const char* path); + QNetworkAccessManager* create(QObject* parent) override; }; QString QMLNetworkAccessFactory::tmpPath = ""; @@ -255,8 +251,9 @@ void dos_qguiapplication_clipboard_setImage(const char* text) void dos_qguiapplication_clipboard_setImageByUrl(const char* url) { - QNetworkAccessManager *manager = new QNetworkAccessManager(); - QObject::connect(manager, &QNetworkAccessManager::finished, [](QNetworkReply *reply) { + auto manager = new QNetworkAccessManager; + manager->setAutoDeleteReplies(true); + QObject::connect(manager, &QNetworkAccessManager::finished, [manager](QNetworkReply *reply) { if(reply->error() == QNetworkReply::NoError) { QByteArray btArray = reply->readAll(); QImage image; @@ -265,8 +262,9 @@ void dos_qguiapplication_clipboard_setImageByUrl(const char* url) QGuiApplication::clipboard()->setImage(image); } else { - qDebug() << "clipboard_setImage:: Downloading image failed!"; + qWarning() << "dos_qguiapplication_clipboard_setImageByUrl: Downloading image failed!"; } + manager->deleteLater(); }); manager->get(QNetworkRequest(QUrl(url))); @@ -278,7 +276,7 @@ void dos_qguiapplication_download_image(const char *imageSource, const char *fil QString fileL = QString(filePath).replace(QRegExp("^(file:/{2})|(qrc:/{2})|(http:/{2})"), ""); // Get current Date/Time information to use in naming of the image file - QString dateTimeString = QDateTime::currentDateTime().toString("dd-MM-yyyy_ hh-mm-ss"); + QString dateTimeString = QDateTime::currentDateTime().toString("dd-MM-yyyy_hh-mm-ss"); // Extract the image data to be able to load and save into a QImage QByteArray btArray = QString(imageSource).split("base64,")[1].toUtf8(); @@ -289,15 +287,15 @@ void dos_qguiapplication_download_image(const char *imageSource, const char *fil void dos_qguiapplication_download_imageByUrl(const char *url, const char *filePath) { - - QNetworkAccessManager *manager = new QNetworkAccessManager(); - QObject::connect(manager, &QNetworkAccessManager::finished, [filePath](QNetworkReply *reply) { + auto manager = new QNetworkAccessManager; + manager->setAutoDeleteReplies(true); + QObject::connect(manager, &QNetworkAccessManager::finished, [manager, filePath](QNetworkReply *reply) { if(reply->error() == QNetworkReply::NoError) { // Extract file path that can be used to save the image QString fileL = QString(filePath).replace(QRegExp("^(file:/{2})|(qrc:/{2})|(http:/{2})"), ""); // Get current Date/Time information to use in naming of the image file - QString dateTimeString = QDateTime::currentDateTime().toString("dd-MM-yyyy_ hh-mm-ss"); + QString dateTimeString = QDateTime::currentDateTime().toString("dd-MM-yyyy_hh-mm-ss"); // Extract the image data to be able to load and save into a QImage QByteArray btArray = reply->readAll(); @@ -307,8 +305,9 @@ void dos_qguiapplication_download_imageByUrl(const char *url, const char *filePa image.save(QString(fileL) + "/image_" + dateTimeString + ".png"); } else { - qDebug() << "download_image:: Downloading image failed!"; + qWarning() << "dos_qguiapplication_download_imageByUrl: Downloading image failed!"; } + manager->deleteLater(); }); manager->get(QNetworkRequest(QUrl(url)));