How can i properly close QCoreApplication in an QT DLL that is using QtNetwork and QThread?

219 Views Asked by At

I'm trying to create a QT DLL to use it in an InnoSetup installer (InnoSetup is written in Delphi Pascal).

This DLL should have a function to download a file from the internet when called from InnoSetup.

The InnoSetup call is made like this:

procedure downloadFile();
  external 'doDownload@files:testdll.dll,libssl-1_1.dll,libcrypto-1_1.dll stdcall loadwithalteredsearchpath delayload';

Then, I call it using this:

function InitializeSetup(): Boolean;
begin
  Result := True;
  ExtractTemporaryFile('testdll.dll');
  downloadFile();
end;

Problem is, i can't reuse downloadFile(); if i need to call it later(after initial call) because on first call, somehow, i suspect, QCoreApplication doesn't properly close and remains in an exec loop. When i call this function a second time, nothing happens, no file is downloading from the internet. The only way to download a file again is to close and reopen Inno Setup.

Here's my code:

TESTDLL.pro

QT -= gui
QT += network

TEMPLATE = lib
DEFINES += TESTCLASS_LIBRARY

CONFIG += c++11 dll

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    testdll.cpp \
    libs\downloadThread.cpp

HEADERS += \
    testdll_global.h \
    testdll.h \
    libs\downloadThread.h

QMAKE_LFLAGS += -Wl,--output-def,testdll.def

# Default rules for deployment.
unix {
    target.path = /usr/lib
}
!isEmpty(target.path): INSTALLS += target

TESTDLL.h

#ifndef TESTDLL_H
#define TESTDLL_H

#include <QtCore>

#include "testdll_global.h"
#include "libs/downloadThread.h"


class TestDLL : public QObject
{
    Q_OBJECT
public slots:
    void handleResults(const QString &);
    void startWThread();
    void initQCoreApp();

private:
    void debugMsg(QString fileName, QString message)
    {
        QFile file(fileName);
        file.open(QIODevice::WriteOnly | QIODevice::Text);
        QTextStream out(&file);
        out << message;
        file.close();
    };
};

#endif

TESTDLL.cpp

#include "testdll.h"

namespace QCoreAppDLL
{
    static int argc = 1;
    static char arg0[] = "testdll.cpp";
    static char * argv[] = { arg0, nullptr };
    static QCoreApplication * pApp = nullptr;
}

extern "C" TESTCLASS_EXPORT void doDownload()
{
    TestDLL a;
    a.initQCoreApp();
}

void TestDLL::startWThread()
{
    downloadThread *thread = new downloadThread(this);
    connect(thread, &downloadThread::finished, thread, &QObject::deleteLater);
    connect(thread, &downloadThread::resultReady, this, &TestDLL::handleResults);
    thread->start();
}

void TestDLL::initQCoreApp()
{

    if (!QCoreApplication::instance())
    {
        QCoreAppDLL::pApp = new QCoreApplication(QCoreAppDLL::argc, QCoreAppDLL::argv);

        TestDLL a;
        a.startWThread();

        QCoreAppDLL::pApp->exec();
    }
}

void TestDLL::handleResults(const QString &)
{
    debugMsg("result.txt","succes!");

    if (QCoreAppDLL::pApp)
        QCoreAppDLL::pApp->quit();
}

DOWNLOADTHREAD.h

#ifndef downloadThread_H
#define downloadThread_H

#include <QtNetwork>
#include <QDebug>

class downloadThread : public QThread
{
    Q_OBJECT
signals:
    void resultReady(const QString &s);
public:
    downloadThread(QObject *parent);
    ~downloadThread();

    void run() override {
        QString result;

        initDownload();

        emit resultReady(result);
    };
    void initDownload();
    void doDownload(QString url);
private:
    QNetworkAccessManager *networkMgr;
    QNetworkReply *_reply;
public slots:
  void replyFinished(QNetworkReply * reply);
};
#endif

DOWNLOADTHREAD.cpp

#include "downloadThread.h"

downloadThread::downloadThread(QObject *parent)
    : QThread(parent)
{
}

downloadThread::~downloadThread()
{
   quit();
   wait();
}

void downloadThread::initDownload()
{
    doDownload("http://www.google.com");
    exec();
}

void downloadThread::doDownload(QString url)
{
    networkMgr = new QNetworkAccessManager;

    QNetworkRequest request;
    request.setUrl(QUrl(url));
    networkMgr->get(request);

    connect(networkMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
}

void downloadThread::replyFinished(QNetworkReply * reply)
{
    if(reply->error() == QNetworkReply::NoError)
    {
        QByteArray content = reply->readAll();
        QSaveFile file("output.txt");
        file.open(QIODevice::WriteOnly);
        file.write(content);
        file.commit();

        reply->deleteLater();
        this->exit();
    } else {
        //error
        reply->deleteLater();
        this->exit();
    }
}

Things i've tried on my own to fix the problem:

  • Use of delete QCoreAppDLL::pApp,
void TestDLL::handleResults(const QString &)
{
    debugMsg("result.txt","succes!");

    if (QCoreAppDLL::pApp){
        QCoreAppDLL::pApp->quit();
        delete QCoreAppDLL::pApp;
    }
}

is crashing the host app.

  • Use of QTimer and SLOT(quit()),
void TestDLL::initQCoreApp()
{

    if (!QCoreApplication::instance())
    {
        QCoreAppDLL::pApp = new QCoreApplication(QCoreAppDLL::argc, QCoreAppDLL::argv);

        TestDLL a;

        connect(&a, SIGNAL(finished()), QCoreAppDLL::pApp, SLOT(quit()));
        QTimer::singleShot(0, &a, SLOT(startWThread));

        QCoreAppDLL::pApp->exec();
    }
}

nothing happens, it's like QCoreApplication exec loop ends before QThread finish the job.

  • Use of Slots/Signals on QThread object,
void TestDLL::startWThread()
{
    downloadThread *thread = new downloadThread(this);
    connect(thread, &downloadThread::finished, thread, &QObject::deleteLater);
    connect(thread, &downloadThread::resultReady, this, &TestDLL::handleResults);
    connect(thread, SIGNAL(finished()), qApp, SLOT(quit()));
    thread->start();
}

that has the same behavior as:

void TestDLL::handleResults(const QString &)
{
    debugMsg("result.txt","succes!");

    if (QCoreAppDLL::pApp)
        QCoreAppDLL::pApp->quit();
}

I'm out of ideas, what else can i do to fix this?

Thanks in advance guys!

1

There are 1 best solutions below

0
JohnnyUSA On

I've finally managed to solve the dilemma by myself. Here it is for those that are interested.

I've created a new function:

void TestDLL::deinitQCoreApp()
{
    if (QCoreAppDLL::pApp)
        delete qApp;
}

and now, my external function becomes:

extern "C" TESTCLASS_EXPORT void doDownload()
{
    TestDLL a;
    a.initQCoreApp();

    TestDLL b;
    b.deinitQCoreApp();
}

Of course, TESTDLL.h has a new entry:

#ifndef TESTDLL_H
#define TESTDLL_H

#include <QtCore>

#include "testdll_global.h"
#include "libs/downloadThread.h"


class TestDLL : public QObject
{
    Q_OBJECT
public slots:
    void handleResults(const QString &);
    void startWThread();
    void initQCoreApp();
    void deinitQCoreApp();

private:
    void debugMsg(QString fileName, QString message)
    {
        QFile file(fileName);
        file.open(QIODevice::WriteOnly | QIODevice::Text);
        QTextStream out(&file);
        out << message;
        file.close();
    };
};

#endif

Now, I can reuse the function how many times I want on the host.

Problem solved!