删除QNetworkReply *回复导致代码之外的SIGSEGV?

时间:2014-09-02 17:51:15

标签: c++ qt sigsegv

我写了一个简单的程序来自动设置路由器的过程。检查后,我发现指针需要deleteQNetworkReply *reply中的replyFinish())并完成工作,但在此之后程序崩溃并且Qt显示它在某些汇编代码中停止: (

所以我的问题是:

  1. 是否有任何常见的做法来处理这种情况(Qt 显示程序在某些汇编代码中停止了)

  2. 删除不再使用的指针时,我做错了什么?     (非常确定)

  3. 以下是代码:

    #ifndef HTTPGETTER_H
    #define HTTPGETTER_H
    class QNetworkAccessManager;
    class QNetworkReply;
    class QNetworkRequest;
    class QAuthenticator;
    #include <QObject>
    
    class httpGetter : public QObject
    {
        Q_OBJECT
    private:
        QNetworkAccessManager *nam;
    public:
        explicit httpGetter(QObject *parent=0);
        ~httpGetter();
    public slots:
        void replyFinish(QNetworkReply* reply);
        void onAuthen(QNetworkReply*,QAuthenticator*);
    };
    

    器具

    #include "httpgetter.h"
    #include <QtNetwork/QNetworkRequest>
    #include <QtNetwork/QNetworkReply>
    #include <QtNetwork/QNetworkAccessManager>
    #include <QAuthenticator>
    #include <QDebug>
    httpGetter::httpGetter(QObject *parent) : QObject(parent)
    {
        nam = new QNetworkAccessManager(this);
        QObject::connect(nam,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinish(QNetworkReply*)));
        QObject::connect(nam,SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),this,SLOT(onAuthen(QNetworkReply*,QAuthenticator*)));
    
        QUrl url("http://192.168.1.1");
        QNetworkReply *reply= nam->get(QNetworkRequest(url));
    
    
    }
    httpGetter::~httpGetter(){
        delete nam;
    }
    void httpGetter::replyFinish(QNetworkReply *reply)
    {
        if (reply==NULL) {
            qDebug() << "NULL reply";
            return;
        }
    
        QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    
        qDebug() << statusCode.toString();
    
        if (reply->error()==QNetworkReply::NoError) {
            qDebug() << "NICE reply";
            QByteArray bytes = reply->readAll();
            QString answer = QString(bytes);
            qDebug()<< answer;
        }
    
        else {
            qDebug() << "reply error";
        }
    
        //delete reply; ==> delele cause sigsegv, if don't the program run like a     while(true)
        //reply == NULL;
    
    }
    
    void httpGetter::onAuthen(QNetworkReply* rep,QAuthenticator* auth)
    {
        if ( rep==NULL || auth == NULL) {
            qDebug()<< "reply or authentication pointer is null";
            return;
        }
    
        qDebug()<< rep->readAll();
        auth->setUser("rolan");
        auth->setPassword("123456");
    }
    

    #include <QtCore/QCoreApplication>
    #include "httpgetter.h"
    
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        httpGetter abc;
    
    
    
    
        return a.exec();
    }
    

1 个答案:

答案 0 :(得分:5)

根据the documentation

  

注意:请求完成后,由...负责   用户在适当的时间删除QNetworkReply对象。不要   直接在连接到finished()的插槽内删除它。您可以   使用deleteLater()函数。

此外,自

nam = new QNetworkAccessManager(this);

创建一个新的QNetworkAccessManager,其中this为其父级,而QObject的析构函数会自动删除其所有子级,delete nam;位于{{ 1}}析构函数是不必要的,最终会导致双重删除。