Results 1 to 8 of 8

Thread: live time of objects send via signals

  1. #1
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default live time of objects send via signals

    HI,

    I noticed that in our application there are a lot of memory leaks when heap-objects, taht are send around via a Qt signal are not destroyed properly.
    And it dont really see a good solution here...


    Qt Code:
    1. public signal:
    2. void mysig(myob* x);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. ....
    2. myob* x = new myob();
    3. emit mysig(x);
    4. ....
    To copy to clipboard, switch view to plain text mode 

    When am i supposed to delete the "x" argument?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: live time of objects send via signals

    It is not simple as it looks, there are certain things to consider.

    You may at first thing that one can delete the object in the slot, but wait
    1. What if no slots are connected ?
    2. What if multiple slots are connected ?

    Then one may think of deleting it after emiting the signal, but wait
    1. What if the a connection is made between QThreads and is qued ?

    If this object is QObject based object, one simple option is to connect to deleteLater() slot. (Note to make this connection as the last connection), so once all the connected slots are executed the object will delete itself.

    conclusion, there may not be a standard way, it highly depends on the scope of the object which you want to achieve.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: live time of objects send via signals

    One way of dealing with it is making sure each object has a parent, and have that object deleted at the latest in the parents destructor.
    If you parent to a QObject, you wont need to think about destruction, all QObjects destroy their children when they get destroyed.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  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: live time of objects send via signals

    Quote Originally Posted by Santosh Reddy View Post
    Note to make this connection as the last connection), so once all the connected slots are executed the object will delete itself.
    It doesn't have to be the last connection. deleteLater() does not delete the object, it merely posts an event to the event loop that the object is to be destroyed when the event is processed. Therefore when you call deleteLater() doesn't matter that much, you can even call it manually, without a signal, e.g.:
    Qt Code:
    1. myob* x = new myob();
    2. emit mysig(x);
    3. x->deleteLater();
    To copy to clipboard, switch view to plain text mode 

    Note that passing such object via a signal to a different thread is forbidden anyway since QObjects are not thread-safe.

    If the object is not a descendant of QObject, I'd suggest passing it by value rather than by pointer. Then regular C++ scope rules apply.
    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
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: live time of objects send via signals

    One way of dealing with it is making sure each object has a parent, and have that object deleted at the latest in the parents destructor.
    If you parent to a QObject, you wont need to think about destruction, all QObjects destroy their children when they get destroyed.
    IMO, one problem with having the parent take care of deleting the child, is when the parent is long lived, there may be multiple children created and will just keep occupying the memory even if they are not used any more. I think the OP wants a solution for short lived objects.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: live time of objects send via signals

    [QUOTE=tuli;233653]And it dont really see a good solution here...
    [/CODE]

    The solution is called smart pointers and the Qt class you are looking for is QSharedPointer

    Cheers,
    _

  7. #7
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: live time of objects send via signals

    The solution is called smart pointers and the Qt class you are looking for is QSharedPointer
    it certainly seems so!
    Although passing by value is suitable in some cases, too.

    THanks for the input, everyone, much appreciated!

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: live time of objects send via signals

    IMO, one problem with having the parent take care of deleting the child, is when the parent is long lived, there may be multiple children created and will just keep occupying the memory even if they are not used any more.
    One solution does not fit all.
    There are many possible solutions, and the the best one is the one that best suits the case at hand, which the OP didn't fully disclose.
    I think the OP wants a solution for short lived objects.
    You maybe right, and maybe wrong, I don't see any indication to support either way.

    Any of the offered solutions can be used, which one depends on the problem and design.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. QDBus : Send an array of objects (C++)
    By lordjoseph in forum Qt Programming
    Replies: 6
    Last Post: 26th January 2012, 11:22
  2. Problems with QNetworkRequest, it does not send signals.
    By nilhcraiv in forum Qt Programming
    Replies: 5
    Last Post: 21st October 2011, 20:00
  3. Replies: 7
    Last Post: 7th June 2010, 14:13
  4. Replies: 4
    Last Post: 2nd December 2008, 16:44
  5. send OS signals to apps on all platforms
    By Morea in forum Qt Programming
    Replies: 3
    Last Post: 21st November 2008, 22:00

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.