Getting SIGSEGV on "finished" signal from QNetwowkAccessManager



  • Hi everyone,

    long time lurker, first time poster. :-)

    I'm using QT 5.4.1 under Windows7-64.

    I have a serious problem with QNetworkAccessManager: I got SIGSEGV when the "finished" signal fires, but only in Debug mode!! :-(

    My sample:

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QNetworkAccessManager>
    #include <QNetworkReply>
    #include <QNetworkRequest>
    #include <QFile>
    #include <QDebug>

    #define TEST_URL "http://www.gnu.org/licenses/gpl-3.0.txt"

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;

    QNetworkAccessManager* mpNetManager;
    
    void initializeNetwork();
    
    void testDownload();
    

    private slots:
    void downloadFinished(QNetworkReply*);
    };

    #endif // MAINWINDOW_H

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    initializeNetwork();
    testDownload();
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    mpNetManager->deleteLater();
    }

    void MainWindow::initializeNetwork()
    {
    mpNetManager = new QNetworkAccessManager;
    }

    void MainWindow::testDownload()
    {
    if (mpNetManager)
    {
    QNetworkRequest netRequest;
    QUrl downloadURL(TEST_URL);
    netRequest.setUrl(downloadURL);
    connect(mpNetManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished()));
    mpNetManager->get(netRequest);
    }
    }

    void MainWindow::downloadFinished (QNetworkReply* pNetReply)
    {
    if (pNetReply)
    {
    if(pNetReply->error())
    {
    qDebug() << "ERROR!";
    qDebug() << pNetReply->errorString();
    }
    else
    {
    QString qsDownloadPath = "downloaded.txt";
    QFile *pOutFile = new QFile(qsDownloadPath);
    if(pOutFile->open(QFile::WriteOnly))
    {
    pOutFile->write(pNetReply->readAll());
    pOutFile->flush();
    pOutFile->close();
    }
    delete pOutFile;
    qDebug() << "Download finished";
    qDebug() << "Content written to";
    qDebug() << qsDownloadPath;
    }
    pNetReply->deleteLater();
    }
    }

    I'm getting mad: it runs fine in Release, but when i launch the debugger, I get the message

    "The inferior stopped because it received a signal from the operating system
    Signal name: SIGSEGV
    Signal meaning: Segmentation Fault"

    I'm not new to Qt, but I'm new to everything QNetwork. :-(

    Thanks a lot!

    Fernando


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    connect(mpNetManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished()));

    You are missing the parameter of downloadFinished



  • You're right, sorry.

    Corrected .cpp source (still the same problem):

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        initializeNetwork();
        testDownload();
    }
    
    
    MainWindow::~MainWindow()
    {
        delete ui;
        mpNetManager->deleteLater();
    }
    
    
    void MainWindow::initializeNetwork()
    {
        mpNetManager = new QNetworkAccessManager;
    }
    
    
    void MainWindow::testDownload()
    {
        if (mpNetManager)
        {
            QNetworkRequest netRequest;
            QUrl downloadURL(TEST_URL);
            netRequest.setUrl(downloadURL);
            connect(mpNetManager, SIGNAL(finished(QNetworkReply * )), this, SLOT(downloadFinished(QNetworkReply * )));
            mpNetManager->get(netRequest);
        }
    }
    
    
    void MainWindow::downloadFinished (QNetworkReply* pNetReply)
    {
        if (pNetReply)
        {
            if(pNetReply->error())
            {
                    qDebug() << "ERROR!";
                    qDebug() << pNetReply->errorString();
            }
            else
            {
                QString    qsDownloadPath = "downloaded.txt";
                QFile *pOutFile = new QFile(qsDownloadPath);
                if(pOutFile->open(QFile::WriteOnly))
                {
                    pOutFile->write(pNetReply->readAll());
                    pOutFile->flush();
                    pOutFile->close();
                }
                delete pOutFile;
                    qDebug() << "Download finished";
                    qDebug() << "Content written to";
                    qDebug() << qsDownloadPath;
            }
            pNetReply->deleteLater();
        }
    }
    

    [edit: Added missing coding tags SGaist]


  • Lifetime Qt Champion

    What do you get if you run your application through a debugger ?



  • As I stated, when i launch the debugger, as soon as the "finished" signal fires I get the message

    "The inferior stopped because it received a signal from the operating system
    Signal name: SIGSEGV
    Signal meaning: Segmentation Fault"

    This is from C:\Windows\SysWOW64\ntdll.dll, instruction "rtlmovmemory"

    This only happens while debugging: the program runs fine in Release.


  • Lifetime Qt Champion

    Can you check with a tool like Dependency Walker the libraries you are currently linking to ?



  • Sure:

    LIBGCC_S_DW2-1.DLL
    QT5CORED.DLL
    QT5NETWORKD.DLL
    QT5WIDGETSD.DLL
    API-MS-WIN-APPMODEL-RUNTIME-L1-1-0.DLL
    API-MS-WIN-CORE-WINRT-ERROR-L1-1-0.DLL
    API-MS-WIN-CORE-WINRT-L1-1-0.DLL
    API-MS-WIN-CORE-WINRT-ROBUFFER-L1-1-0.DLL
    API-MS-WIN-CORE-WINRT-STRING-L1-1-0.DLL
    API-MS-WIN-SHCORE-SCALING-L1-1-1.DLL
    DCOMP.DLL
    IESHIMS.DLL


  • Lifetime Qt Champion

    That part looks good

    Then let's get step by step. Does it also fail if you comment out the network stuff ?



  • It debugs nicely without

    mpNetManager->get(netRequest);

    And it runs nicely (even with mpNetManager->get(netRequest)) when not debugging (even if the build was in debug mode).
    To me, it's a bug in the debugger :-) , at least under Windows7-64.


  • Lifetime Qt Champion

    Using Qt's MinGW package ?



  • Sorry, I was out of town.

    Yes, using Qt's MinGW package (I don't have and don't like Visual Studio :-) ).


  • Lifetime Qt Champion

    So you have MinGW 4.9 ?



  • Yes, MinGW 4.9.1.
    Do you think I should file a bug report?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.