Results 1 to 10 of 10

Thread: deleteLater?

  1. #1
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default deleteLater?

    Hello,

    I have a class CTest which has many instances of some class called CNetworkManager.
    e.g., many functions in CTest create CNetworkManager objects. e.g.,
    Qt Code:
    1. void CTest:f1()
    2. {
    3. CNetworkManager *p = new CNetworkManager();
    4. //...do some stuff
    5. // e.g., makePostRequest
    6.  
    7. }
    8. void CTest:f2()
    9. {
    10. CNetworkManager *p = new CNetworkManager();
    11. //...do some other stuff
    12. //e.g., makePostRequest
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 
    CNetworkManager is used for network communication. Inside it uses a QNetworkAccessManager
    object for that. I usually process a server response in the requestFinished of the CNetworkManager
    class and call a CTest method (as callback -- and also pass the data from the server).

    You can see I may have many instances of CNetworkManager in my app. My question is where
    is it safe to delete these CNetworkManager objects ???? and how?

    Thank you.

  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: deleteLater?

    You can delete them once you're done using them either by an explicit delete call or with deleteLater().
    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
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: deleteLater?

    Quote Originally Posted by wysota View Post
    You can delete them once you're done using them either by an explicit delete call or with deleteLater().
    Hi, thanks for your response.
    In the requestFinished of the CNetworkManager class I invoke a callback from my CTest as I mentioned, e.g., like this:
    Qt Code:
    1. void CNetworkManager::requestFinished(QNetworkReply* reply)
    2. {
    3. //...
    4. this->ptCallBackCTest(ptObject, netResponse);
    5. // (*) we will get here - to line number 5 - after the callback finished execution right? So, if I delete here the CNetworkManager instance or "this" in other
    6. // words it means I am "killing myself" within my own function.
    7. //..
    8. }
    To copy to clipboard, switch view to plain text mode 

    In other way, I could also pass a pointer to "this" (above code) to the callback function and try to delete it inside the CTest callback -- but in that
    case I will never reach the 5th line as above -- and will not it cause crash??? Thanks.

  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: deleteLater?

    I don't understand why you'd want to kill the manager when a request finishes. How do you know that no more requests are coming?
    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
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: deleteLater?

    Quote Originally Posted by wysota View Post
    How do you know that no more requests are coming?
    Because for each request I create a new CNetworkManager object (maybe this is not recommended but that is how it is now). So each one usually handles one request.

    Quote Originally Posted by wysota View Post
    I don't understand why you'd want to kill the manager when a request finishes
    I don't mind. I am just not sure if it is ok. As it can be seen from my implementation the requestFinished is implemented as a member function of CNetworkManager:
    Qt Code:
    1. void CNetworkManager::requestFinished(QNetworkReply* reply)
    2. {
    3. ...
    4. // Is it ok to call: "delete this" here??
    5. // now we are in the instance created at line 3 of my initial Code snippet.
    6. }
    To copy to clipboard, switch view to plain text mode 
    So I was not sure if it is ok to call delete "this" as above? (e.g., in a member function). is it? Thanks.

  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: deleteLater?

    Quote Originally Posted by ggdev001 View Post
    Because for each request I create a new CNetworkManager object (maybe this is not recommended but that is how it is now). So each one usually handles one request.
    That's pretty... well... unwise... Anyway, if that's the case, you can call deleteLater() in requestFinished. I wouldn't call plain delete though, it'd be very error prone (think what happens if someone else connects to the same signal and tries to access the manager).
    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.


  7. #7
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: deleteLater?

    Quote Originally Posted by wysota View Post
    you can call deleteLater() in requestFinished
    You mean like: this->deleteLater() ??

    Quote Originally Posted by wysota View Post
    think what happens if someone else connects to the same signal
    this should not happen. As I mentioned each CNetworkManager object separately issues only one POST request and waits for that response.

  8. #8
    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: deleteLater?

    Quote Originally Posted by ggdev001 View Post
    You mean like: this->deleteLater() ??
    Yes. Or simply "deleteLater()".

    this should not happen.
    Well... "should not" and "will not" are two different things. Nothing will prevent your collegue (or yourself in two years from now) to write a connect statement that will connect to that signal.
    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.


  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: deleteLater?

    Excuse me for jumping in, but is this a typical use case for deleteLater()? That is, a QObject instance must live long enough for it to be used in a slot or other method, at which point it can be deleted, and that deletion occurs the next time the event loop in which deleteLater() was called is entered?

    What happens in the case where the signal that passes the instance pointer is handled by more than one slot? Does moving from one slot to the next count as a "return to the event loop"? Or is the invocation of all the connected slots happen within the scope of a single event, and only after all slots have been called is the instance deleted?

    If the latter is the case, this is a pretty useful scenario for when stack-based instances won't work.

  10. #10
    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: deleteLater?

    Quote Originally Posted by d_stranz View Post
    Does moving from one slot to the next count as a "return to the event loop"?
    No, it doesn't.
    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. QThread deleteLater()
    By doggrant in forum Qt Programming
    Replies: 7
    Last Post: 17th March 2012, 17:21
  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.