Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[SOLVED] QThread Segfault on Linux
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.
@class Upload : public QThread
// constructor Upload(QObject *parent = 0); bool imageUpload(); void loadImage(QString reply_data);--
#endif // UPLOAD_H
Upload::Upload(QObject *parent) : QThread(parent)
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;
if (this->reply->error() == QNetworkReply::NoError)
QMessageBox::warning(0, "Error", "Unable to upload image.");
I don't know why it crashes. But QNetworkAccessManger works asynchronously by design. You do not need a thread here.
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 :/
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
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.
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?
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.