[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.
upload.h
@class Upload : public QThread
{
Q_OBJECTpublic:
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.");
}@ -
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.
-
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?