Results 1 to 6 of 6

Thread: QNetworkReply::deleteLater problem

  1. #1
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QNetworkReply::deleteLater problem

    I think that there is a problem with deleting QNetworkReply in case of continious communication between the client and server. I have a QEvenLoop-based class that initiates connection to the server, processes reply and repeates this cycle indefinitely.

    In "isFinished" slot connected to corresponding QNetworkAccessManager signal I am calling QNetworkReply->deleteLater(). After 5-10 cycles I am getting an error. Review of the stack indicates that even though isFinished is called by accessmanager, it still wants to write something in networkReply instance later on when the instance is already deleted.

    My workaround is: instead of calling "deleteLater" in "isFinished" slot I store instance of QNetworkReply in the smart pointer. Therefore QNetworReply is released not in the current cycle of communication, but in "next" cycle. My class design looks like this:

    Qt Code:
    1. class myClass: QThread
    2. {
    3. signals:
    4. void sendSomeDataToServer();
    5. slot:
    6. void onSendSomeDataToServer();
    7. ...
    8. boost::shared_ptr<QNetworkReply> _delayedRelease;
    9. QNetworkAccessManager _http;
    10. }
    11.  
    12. myClass::myClass()
    13. {
    14. ...
    15. connect(this, sendSomeDataToServer, this, OnSendSomeDataToServer, Qt::ConnectionType::Queued);
    16. }
    17.  
    18. myClass::run()
    19. {
    20. OnsendSomeDataToServer();
    21. exec();
    22. }
    23.  
    24. myclass::OnsendSomeDataToServer()
    25. {
    26. ...
    27. _http::post();
    28. }
    29. myclass::isFinished(QNetworReply* reply)
    30. {
    31. ...
    32. // reply->deleteLater(); does not work
    33.  
    34. _delayedRelease.reset(reply); // this works, I guess because it allows for some time to actually finish communication.
    35.  
    36. emit sendSomeDataToServer(); // this signal is processed in myclass instance; it is queued.
    37. }
    To copy to clipboard, switch view to plain text mode 
    This workaround does work, but I will appreciate any comments/suggestions. After all, deleteLater() is called where it is supposed to be called based on the documentation for QNetworAccessManager "isFinished" slot. If necessary I can publish the stack and provide any necessary info.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkReply::deleteLater problem

    Your code is flawed. You are accessing the _http variable which lives in the main thread from within the worker thread which is forbidden. I'm not surprised deleteLater() causes a segfault in this situation. If all your thread does is control the QNAM, then get rid of the thread and do it in the main thread, otherwise read about how to use threads properly and correct your code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkReply::deleteLater problem

    May be I misunderstood your comment, but _http is a member variable of "myClass" instance. Why would you say that it is accessed from "worker thread"? Usage of myClass is analogous to this:

    Qt Code:
    1. void main()
    2. {
    3. myClass a;
    4. std::cin >>i;
    5. }
    To copy to clipboard, switch view to plain text mode 
    where a is an instance of event loop. Network signals are processed in the instance "a".
    There is no live worker thread here in a sense that "a" exited from "run" with "exec", leaving even loop alive.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkReply::deleteLater problem

    Quote Originally Posted by TorAn View Post
    May be I misunderstood your comment, but _http is a member variable of "myClass" instance. Why would you say that it is accessed from "worker thread"?
    You access it (or exactly its child, the QNetworkReply) from the run() method, right? It's the worker thred function and the _http variable "lives" in the thread that created the myClass instance.

    There is no live worker thread here in a sense that "a" exited from "run" with "exec", leaving even loop alive.
    I'm afraid I don't understand what you mean here, I'm just reading the source code and I see you are accessing the _http variable from within run() which is ran in a separate thread by QThread::start() that I guess you are calling at some point. Otherwise exec() wouldn't be called and you'd get no event loop.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkReply::deleteLater problem

    I see your point now, thanks. Indeed, _http member variable lives in the main thread when I call a.start(). Run method of the "a" instance uses _http once and then exits with "exec()", leaving event loop alive.

    I will rearrange the code by the scheme below. Do you consider it to be a proper usage of event loop and QNAM?

    Qt Code:
    1. class myClass : public QThread
    2. {
    3. public:
    4. myClass ()
    5. {
    6. connect(this, SIGNAL(doComm()), this, SLOT(onDoComm()), Qt::Queued);
    7. }
    8. void run() { exec(); }
    9. void startComm() {emit doComm();}
    10. signals:
    11. void doComm();
    12. slots:
    13. void onDoComm() {_http.post(...);};
    14. void isFinished(QNetworReply* r) {r->deleteLater(); emit doComm();}
    15. private:
    16. QNAM _http;
    17. };
    18.  
    19. main
    20. {
    21. myClass a;
    22. a.start();
    23. a.startComm();
    24.  
    25. int i;
    26. std::cin >> i;
    27. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkReply::deleteLater problem

    Quote Originally Posted by TorAn View Post
    Run method of the "a" instance uses _http once and then exits with "exec()", leaving event loop alive.
    No. exec() blocks until the event loop quits and only then the thread is terminated.

    I will rearrange the code by the scheme below. Do you consider it to be a proper usage of event loop and QNAM?
    No. In my opinion you don't need the thread at all. Just create the application object and run its event loop.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 10
    Last Post: 14th January 2010, 23:55
  2. To use or not to use: deleteLater()
    By codeslicer in forum Qt Programming
    Replies: 11
    Last Post: 11th July 2009, 21:43
  3. QT deleteLater again.
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2008, 22:36
  4. Qt Deletelater
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2008, 16:58
  5. DeleteLater works... but why?
    By TheGrimace in forum Qt Programming
    Replies: 11
    Last Post: 6th June 2007, 15:14

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.