SIGSEGV produced when using QNetworkManager after/during post
-
Dear Qt dev's,
I have recently started programming again in C++ in combination with Qt, I'm now creating an application and I'm allready stuck.
I have this piece of code in which I have obfuscated the url for privacy reasons:
@
void CopyDroid::PostLinkRequestStatus(const QString &uid, const QString &link_request_value)
{
QNetworkAccessManager *nam = new QNetworkAccessManager();
QNetworkRequest *request = new QNetworkRequest(QUrl("http://obfuscated.com/action.php"));
QByteArray *postData = new QByteArray();
postData->append("action=requestLinkStatus&uid="+uid+"&link_request_value="+link_request_value);
reply = nam->post(*request, *postData);connect(reply, SIGNAL(finished()), this, SLOT(ProcessLinkRequestStatus())); // the line above causes SIGSEGV segmentation fault, why?
}
@I have tried looking at all pointers, but I can't see what is wrong. In another function I'm doing the exact same thing but then it succesfully connects without a segmentation fault. the PostLinkRequestStatus gets called from a QDialog like this:
@
void AddDialog::checkLinkRequestConfirmation()
{
if(count >= 10) {
disconnect(timer, SIGNAL(timeout()), 0, 0);
accept();
} else {
count++;
progressBar->setValue(count);copyDroid->PostLinkRequestStatus("Windows7", linkRequestValueText->text()); if (linkRequestStatus == true) { progressBar->setValue(progressBar->maximum()); disconnect(timer, SIGNAL(timeout()), 0, 0); accept(); } qDebug() << "sleeping... replace this with checking for linkRequestConfirmation..."; }
}
@the timer is started, then checkLinkRequestConfirmation is called. I want to perform PostLinkRequestStatus which will do a http request and connect the finished signal of a QNetworkReply to another function which will then process the xml and according to the value in the xml, the linkRequestStatus must be set to true.
The dialog function gets called by the timer timeout signal, so that when the linkRequestStatus is set to true the progressBar will fill out to maximum and the dialog can accept.
What am I doing wrong here? Please guide me on my way to learning Qt!
~Dino
-
Hi and welcome to DevNet!
The connect line looks quite ok at first sight. If you set a breakpoint on that line, does the reply pointer actually have a valid address?
Some general notes:
- the QNetworkAccessManager should be created in the class' constructor. You don't need a new instance on every request, but can reuse that one instance.
- the QNetworkRequest and the QByteArray variables are supposed to be created on the stack, hence no pointer variable and no new (QByteArray postData = ....)
-
Dear Volker,
Thank you for your quick reply!
Yes reply is actually pointing to a valid address. This segfault show me once again that the line a segfault is show by the debugger does not have to do a single thing with the actual error.
Your general notes are very good use to me, my Qt skills have grown a little bit thanks to your advise.
I have solved the problem, I will tell you how:
First I created a new project and reprogrammed the QTimer driven "loop" where supposedly the segmentation fault happend. I was not able to reproduce, so I knew then that I made some other error.
I then went back to my original project and started stripping it down to a bare bone useless version with just some control statements and minimal gui. I then stumbled upon these lines:
@
void AddDialog::setCopyDroid(CopyDroid *copyDroid)
{
copyDroid = copyDroid;
copyDroid->PostLinkRequest("Windows7");
}
@This is such a terrible late-night-programming-error.... I changed it to:
@this->copyDroid = copyDroid;
@It now works! And I am very happy with the results! When the application is done, I will come back and happily report how my Qt-quest is progressing!
Again thanks a lot for welcomming me and replying so quickly!
~Dino
-
Thanks for reporting back. We all know that nasty little erros too good. It so often that it's these tiny typos that make you scratch your head for hours :-)
Good luck with your further progress and of course you're very welcome to ask again if needed.