Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QThread Segfault on Linux
QtWS25 Last Chance

[SOLVED] QThread Segfault on Linux

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 4.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jmper
    wrote on last edited by
    #1

    So I've got an issue with QThread on Linux I can't for the life of me figure out why this one section within my QThread segfaults.

    upload.h
    @class Upload : public QThread
    {
    Q_OBJECT

    public:
    QByteArray m_uploaddata;

    // constructor
    Upload(QObject *parent = 0);
    
    bool imageUpload();
    void loadImage(QString reply_data);--
    

    protected:
    void run();

    private slots:
    void finished_upload();

    private:
    QNetworkAccessManager *net;
    QNetworkReply *reply;

    };

    #endif // UPLOAD_H
    @

    upload.cpp
    @#include "upload.h"

    Upload::Upload(QObject *parent) : QThread(parent)
    {
    // yar?
    }

    void Upload::run()
    {
    imageUpload();
    exec();
    }

    bool Upload::imageUpload()
    {
    net = new QNetworkAccessManager();

    // setup random image name;
    QString file = randomString(15) + ".png";
    
    QNetworkRequest httpReq;
    httpReq.setUrl(QUrl("http://pixldrop.com/index.php/upload/drop/"));
    
    QString bound("---------------------------723690991551375881941828858");
    QByteArray data(QString("--"+bound+"\r\n").toAscii());
    data += "Content-Disposition: form-data; name=\"action\"\r\n\r\n";
    data += "\r\n";
    data += QString("--" + bound + "\r\n").toAscii();
    
    data += "Content-Disposition: form-data; name=\"file\"; filename=\"" + file +"\"\r\n";
    data += "Content-Type: image/png\r\n\r\n";
    data += m_uploaddata;
    
    data += "\r\n";
    data += QString("--" + bound + "\r\n").toAscii();
    
    httpReq.setRawHeader(QString("Accept-Encoding").toAscii(), QString("gzip,deflate").toAscii());
    httpReq.setRawHeader(QString("Content-Type").toAscii(),QString("multipart/form-data; boundary=" + bound).toAscii());
    httpReq.setRawHeader(QString("Content-Length").toAscii(), QString::number(data.length()).toAscii());
    
    ////////////////////////////////////////////////////////////////////
    // Segfault on next line /////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////
    this->reply = net->post(httpReq, data);      ////////  <- this line causes the segfault every time :(
    
    
    connect(this->reply, SIGNAL(finished()), this, SLOT(finished_upload()));
    
    return true;
    

    }

    void Upload::finished_upload()
    {
    if (this->reply->error() == QNetworkReply::NoError)
    loadImage(QString(this->reply->readAll()));
    else
    QMessageBox::warning(0, "Error", "Unable to upload image.");
    }@

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      I don't know why it crashes. But QNetworkAccessManger works asynchronously by design. You do not need a thread here.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jmper
        wrote on last edited by
        #3

        Really? so for example if I wanted this upload to start in the background and continue, until done even if the user closes the main app window etc... it would do that?

        Just as a side note that exact same code works with no issues on windows :/

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          I would have to try it out. On a first glance, the code does not look fishy.

          QNAM will work as you expect. The call to get or post immediately returns and you can continue with your regular work.

          You will in any case - using a thread or the QNAM built in asynchronity - have to make sure, that your application and the QNAM object survive the close of your main window. That means you must not make the QNAM or thread a member of your main window and set the parent, as it would be destroyed once your main window destructor is called upon closing it. Additionally, you will need to call

          @
          qApp->setQuitOnLastWindowClosed(false);
          @

          in order to prevent the application to shut down on the close of the last window.

          If you absolutely want to use QThread, make sure to read Peppes excellent article on "Threads, Events and QObjects":/wiki/Threads_Events_QObjects.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jmper
            wrote on last edited by
            #5

            After looking more into this the issue has grown I can't get any signals to connect to any slots on linux?! Has anyone else ever had any issues like this? I even did just a test app with a simple get and read some static data back but NONE of the signals ever get hit?

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              Can you provide us the source of your test app?

              Also, see the DocNotes at the end of the [[Doc:QNetworkAccessManager]] API docs for same short, yet complete samples on how to use QNAM.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved