Results 1 to 4 of 4

Thread: QNetworkAccessManager->Post() causes SIGSEGV on exit

  1. #1
    Join Date
    Nov 2016
    Posts
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QNetworkAccessManager->Post() causes SIGSEGV on exit

    Wow, I need help, I have no clue on what is going on. Thanks in advance!

    I have a QGUIApplication, getting a singleton from class X. Class X has a QNetworkAccessManager.

    I don't use the Post() method, it's all good when I close the window. But, as soon as I use the Post method(), when I close the window, I get a segfault. The only info I get from the stack is this:
    ntdll!RtlFreeHeap
    ucrtbase!free
    LIBEAY32!CRYPTO_free

    LIBEAY32!CRYPTO_free seems to be related to SSL. I have nothing about SSL in my code.

    In the code, Post() works fine, I'm posting a QByteArray, I receive the reply in the slot called by the Finished() signal.

    Here some code:


    Main.cpp
    Qt Code:
    1. //AboutToClose() calls Finalize() in ClassX
    2. QObject::connect(&app, SIGNAL(aboutToQuit()), &OtherClass, SLOT(AboutToClose()));
    To copy to clipboard, switch view to plain text mode 

    Class X.h
    Qt Code:
    1. #include <QNetworkAccessManager>
    2. #include <QNetworkReply>
    3.  
    4. private:
    5. QNetworkAccessManager* m_Manager;
    6.  
    7. public Q_SLOTS:
    8. void onFinished(QNetworkReply* pReply);
    To copy to clipboard, switch view to plain text mode 

    Class X.cpp
    Qt Code:
    1. void ClassX::InitMyStuff()
    2. {
    3. m_Manager = new QNetworkAccessManager(this);
    4.  
    5. if(!connect(m_Manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*)))) { ASSERT(FALSE); }
    6.  
    7. PostData();
    8. }
    9.  
    10. void ClassX::PostData()
    11. {
    12. QFile file(...);
    13. ...file.open(QIODevice::ReadOnly)...
    14.  
    15. QString sUrl(BASE_URL);
    16.  
    17. QNetworkRequest request = QNetworkRequest(QUrl(sUrl));
    18. request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlenconded");
    19.  
    20. m_Manager->post(request, file.readAll());
    21.  
    22. file.close();
    23. }
    24.  
    25. void ClassX::onFinished(QNetworkReply *pReply)
    26. {
    27. pReply->close();
    28. pReply->deleteLater();
    29. }
    30.  
    31. // Look above at Main.cpp to see how this is called
    32. void ClassX::Finalize()
    33. {
    34. m_Manager->disconnect();
    35. delete m_Manager;
    36. m_Manager = NULL;
    37. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 16th November 2016 at 11:08. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,310
    Thanks
    244
    Thanked 678 Times in 668 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QNetworkAccessManager->Post() causes SIGSEGV on exit

    You have created m_Manager as a child of your ClassX instance. In your Finalize() method, you delete the m_Manager instance, but you don't tell ClassX you have done that, so when the ClassX instance goes out of scope, it tries to delete something that is already gone (or perhaps does something else, referencing an invalid pointer). You can either:

    - create m_Manager with no parent
    - call deleteLater() in Finalize() rather than operator delete()
    - do nothing except remove the delete() call.

    In the last case, since m_Manager is created with the ClassX instance as a parent, it will automatically be deleted when the ClassX instance is. If the only place you call Finalize() is when you are exiting the app, then all that needs to happen there is to call QNetworkAccessManager::disconnect() and maybe not even that, since I would expect the QNetworkAccessManager destructor to disconnect any open connections.

    It almost always causes a SIGSEGV when you call delete on a QObject instance that is owned by something else because that doesn't give Qt the opportunity to clean up after itself before the pointer becomes invalid. If you have to delete something, better to call deleteLater() and let it occur during the normal course of Qt event processing.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager->Post() causes SIGSEGV on exit

    Quote Originally Posted by d_stranz View Post
    You have created m_Manager as a child of your ClassX instance. In your Finalize() method, you delete the m_Manager instance, but you don't tell ClassX you have done that, so when the ClassX instance goes out of scope, it tries to delete something that is already gone (or perhaps does something else, referencing an invalid pointer). You can either:
    That should be ok as long as the delete happens before the parent/child cleanup.

    QObjects inform their parent when they are being deleted and the parent removes them from its children list.

    Cheers,
    _

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,310
    Thanks
    244
    Thanked 678 Times in 668 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QNetworkAccessManager->Post() causes SIGSEGV on exit

    That should be ok as long as the delete happens before the parent/child cleanup.
    Yes, but I'm guessing that there is something event loop-dependent that using operator delete() doesn't allow to happen before the instance is gone, and this is what is causing the SIGSEGV as control returns to the event loop. I am superstitious enough that I almost always use deleteLater() on QObject instances that I manually remove unless I know for sure it is safe to delete them directly.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Again QNetworkAccessManager POST and PHP
    By ZSWASW in forum Qt Programming
    Replies: 2
    Last Post: 2nd March 2014, 20:38
  2. QNetworkAccessManager post
    By januszmk in forum Newbie
    Replies: 2
    Last Post: 13th April 2012, 10:01
  3. POST and QNetworkAccessManager
    By hakermania in forum Newbie
    Replies: 1
    Last Post: 13th February 2011, 01:05
  4. QNetworkAccessManager::post() never returns
    By danc81 in forum Qt Programming
    Replies: 2
    Last Post: 21st October 2009, 10:13
  5. QNetworkAccessManager double post
    By QPlace in forum Qt Programming
    Replies: 1
    Last Post: 11th February 2009, 05:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.