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 ```
This commit is contained in:
parent
b8fc19d38f
commit
a7e82bfec7
|
@ -38,13 +38,11 @@
|
||||||
#include <QtGui/QIcon>
|
#include <QtGui/QIcon>
|
||||||
#include <QtQml/QQmlContext>
|
#include <QtQml/QQmlContext>
|
||||||
#include <QtQml/QQmlNetworkAccessManagerFactory>
|
#include <QtQml/QQmlNetworkAccessManagerFactory>
|
||||||
#include <QtCore>
|
|
||||||
#include <QClipboard>
|
#include <QClipboard>
|
||||||
#include <QtGui/QPixmap>
|
#include <QtGui/QPixmap>
|
||||||
#include <QtGui/QImage>
|
#include <QtGui/QImage>
|
||||||
#include <QtGui/QColorSpace>
|
#include <QtGui/QColorSpace>
|
||||||
#include <QtGui/QTextDocumentFragment>
|
#include <QtGui/QTextDocumentFragment>
|
||||||
#include <QtCore/QFile>
|
|
||||||
#include <QtCore/QUuid>
|
#include <QtCore/QUuid>
|
||||||
#include <QtQml/QQmlApplicationEngine>
|
#include <QtQml/QQmlApplicationEngine>
|
||||||
#include <QtQuick/QQuickView>
|
#include <QtQuick/QQuickView>
|
||||||
|
@ -97,7 +95,7 @@ void register_meta_types()
|
||||||
}
|
}
|
||||||
|
|
||||||
// jrainville: I'm not sure where to put this, but it works like so
|
// 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
|
class QMLNetworkAccessFactory : public QQmlNetworkAccessManagerFactory
|
||||||
{
|
{
|
||||||
|
@ -110,9 +108,7 @@ class QMLNetworkAccessFactory : public QQmlNetworkAccessManagerFactory
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QNetworkAccessManager* create(QObject* parent);
|
QNetworkAccessManager* create(QObject* parent) override;
|
||||||
|
|
||||||
void setTmpPath(const char* path);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
QString QMLNetworkAccessFactory::tmpPath = "";
|
QString QMLNetworkAccessFactory::tmpPath = "";
|
||||||
|
@ -255,8 +251,9 @@ void dos_qguiapplication_clipboard_setImage(const char* text)
|
||||||
|
|
||||||
void dos_qguiapplication_clipboard_setImageByUrl(const char* url)
|
void dos_qguiapplication_clipboard_setImageByUrl(const char* url)
|
||||||
{
|
{
|
||||||
QNetworkAccessManager *manager = new QNetworkAccessManager();
|
auto manager = new QNetworkAccessManager;
|
||||||
QObject::connect(manager, &QNetworkAccessManager::finished, [](QNetworkReply *reply) {
|
manager->setAutoDeleteReplies(true);
|
||||||
|
QObject::connect(manager, &QNetworkAccessManager::finished, [manager](QNetworkReply *reply) {
|
||||||
if(reply->error() == QNetworkReply::NoError) {
|
if(reply->error() == QNetworkReply::NoError) {
|
||||||
QByteArray btArray = reply->readAll();
|
QByteArray btArray = reply->readAll();
|
||||||
QImage image;
|
QImage image;
|
||||||
|
@ -265,8 +262,9 @@ void dos_qguiapplication_clipboard_setImageByUrl(const char* url)
|
||||||
QGuiApplication::clipboard()->setImage(image);
|
QGuiApplication::clipboard()->setImage(image);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
qDebug() << "clipboard_setImage:: Downloading image failed!";
|
qWarning() << "dos_qguiapplication_clipboard_setImageByUrl: Downloading image failed!";
|
||||||
}
|
}
|
||||||
|
manager->deleteLater();
|
||||||
});
|
});
|
||||||
|
|
||||||
manager->get(QNetworkRequest(QUrl(url)));
|
manager->get(QNetworkRequest(QUrl(url)));
|
||||||
|
@ -289,9 +287,9 @@ void dos_qguiapplication_download_image(const char *imageSource, const char *fil
|
||||||
|
|
||||||
void dos_qguiapplication_download_imageByUrl(const char *url, const char *filePath)
|
void dos_qguiapplication_download_imageByUrl(const char *url, const char *filePath)
|
||||||
{
|
{
|
||||||
|
auto manager = new QNetworkAccessManager;
|
||||||
QNetworkAccessManager *manager = new QNetworkAccessManager();
|
manager->setAutoDeleteReplies(true);
|
||||||
QObject::connect(manager, &QNetworkAccessManager::finished, [filePath](QNetworkReply *reply) {
|
QObject::connect(manager, &QNetworkAccessManager::finished, [manager, filePath](QNetworkReply *reply) {
|
||||||
if(reply->error() == QNetworkReply::NoError) {
|
if(reply->error() == QNetworkReply::NoError) {
|
||||||
// Extract file path that can be used to save the image
|
// Extract file path that can be used to save the image
|
||||||
QString fileL = QString(filePath).replace(QRegExp("^(file:/{2})|(qrc:/{2})|(http:/{2})"), "");
|
QString fileL = QString(filePath).replace(QRegExp("^(file:/{2})|(qrc:/{2})|(http:/{2})"), "");
|
||||||
|
@ -307,8 +305,9 @@ void dos_qguiapplication_download_imageByUrl(const char *url, const char *filePa
|
||||||
image.save(QString(fileL) + "/image_" + dateTimeString + ".png");
|
image.save(QString(fileL) + "/image_" + dateTimeString + ".png");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
qDebug() << "download_image:: Downloading image failed!";
|
qWarning() << "dos_qguiapplication_download_imageByUrl: Downloading image failed!";
|
||||||
}
|
}
|
||||||
|
manager->deleteLater();
|
||||||
});
|
});
|
||||||
|
|
||||||
manager->get(QNetworkRequest(QUrl(url)));
|
manager->get(QNetworkRequest(QUrl(url)));
|
||||||
|
|
Loading…
Reference in New Issue