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. SIGSEGV produced when using QNetworkManager after/during post
Forum Updated to NodeBB v4.3 + New Features

SIGSEGV produced when using QNetworkManager after/during post

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.9k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    dhensen
    wrote on last edited by
    #1

    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

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

      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 = ....)

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

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dhensen
        wrote on last edited by
        #3

        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

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

          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.

          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