Results 1 to 8 of 8

Thread: postEvent() to a non-existing receiver

  1. #1
    Join Date
    Jan 2006
    Location
    Germany, Rostock
    Posts
    17
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default postEvent() to a non-existing receiver

    Hi all,

    when posting a custom QEvent to a receiver that has been removed I'll get a debug message "Receiver is not a valid QObject".

    But, how do I identify this problem inside my application?

    postEvent() doesn't have a return and the event itself has been removed in that case

    Any suggestions?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: postEvent() to a non-existing receiver

    How do you remove that receiver? Do you use deleteLater()?

  3. #3
    Join Date
    Jan 2006
    Location
    Germany, Rostock
    Posts
    17
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: postEvent() to a non-existing receiver

    Jacek,

    I implemented a generic event subscription to provide async triggering between application components. The goal of this pattern is that underlying components do not have to know the (high level) components. Of course, all receivers will unregister, when they disappear (e.g. in destructor). But sometime this will not work. My intention is to clean-up the subscription list in a generic way if the receiver object isn't alive anymore.

    Thus, I can’t specify whether the receiver was destroyed by calling deleteLater() or somehow else.

    Artschi

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: postEvent() to a non-existing receiver

    Quote Originally Posted by Artschi
    My intention is to clean-up the subscription list in a generic way if the receiver object isn't alive anymore.
    You might try QPointer or QObjectCleanupHandler, but I'm not sure if they are thread-safe.

    Quote Originally Posted by Artschi
    Thus, I can’t specify whether the receiver was destroyed by calling deleteLater() or somehow else.
    AFAIR I read somewhere that you shouldn't delete an object if there are some events for it in the event queue, but I can't find it anywhere now. Maybe it was only about QWidgets? Anyway deleteLater() won't help you much in a multi-threaded application.

  5. The following user says thank you to jacek for this useful post:

    Artschi (30th May 2006)

  6. #5
    Join Date
    Jan 2006
    Location
    Germany, Rostock
    Posts
    17
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: postEvent() to a non-existing receiver

    OK, these are two aspects of my problem:
    1. How to avoid leaked pointers to receiver objects
      Quote Originally Posted by jacek
      You might try QPointer or QObjectCleanupHandler, but I'm not sure if they are thread-safe.
      QPointer would be perfect if all receiver objects are of the same type. Unfortunately, my EventDispatcher has to handle different receivers

      QObjectCleanupHandler sounds interesting. I'll verify this.

      Another approach is to use the QObject::destroyed signal that could be connected to a clean-up slot in the dispatcher.
      .
    2. How to handle receiver objects
      Quote Originally Posted by jacek
      AFAIR I read somewhere that you shouldn't delete an object if there are some events for it in the event queue, but I can't find it anywhere now. Maybe it was only about QWidgets? Anyway deleteLater() won't help you much in a multi-threaded application.
      I understand from the documentation that the purpose of QObject::deleteLater() is to enable object deletion from outside the thread, irrespective if that object is executing an event at this moment.

      From inside the thread I'm able to avoid this conflict. And even there are events left in the queue for that receiver, QT handles this. However, this is not documented and I don't know if it will work in any case ?!

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: postEvent() to a non-existing receiver

    Quote Originally Posted by Artschi
    I understand from the documentation that the purpose of QObject::deleteLater() is to enable object deletion from outside the thread, irrespective if that object is executing an event at this moment.
    deleteLater() tells Qt to delete the object when program flow returns to the event loop, but it looks like it isn't thread-safe, so you should forget about it.

  8. The following user says thank you to jacek for this useful post:

    Artschi (1st June 2006)

  9. #7
    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: postEvent() to a non-existing receiver

    It's usefull if, for example, you want to delete the calling object from within a slot.

  10. The following user says thank you to wysota for this useful post:

    Artschi (1st June 2006)

  11. #8
    Join Date
    Jan 2006
    Location
    Germany, Rostock
    Posts
    17
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb Re: postEvent() to a non-existing receiver

    Thanks for your support.

    At the end I decided to connect the destroyed() signal of each receiver with a cleanUp() slot of my EventDispatcher during event subscription.

    This works quite good.

    Still left to check what happens if the receiver will be deleted before it is connected.

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.