Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Bug with `findChildren()` and `deleteLater()` causes crash
Qt 6.11 is out! See what's new in the release blog

Bug with `findChildren()` and `deleteLater()` causes crash

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 739 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Developer123
    wrote on last edited by
    #1

    I query some components from the UI sometimes using findChildren() and call their methods using QMetaObject::invokeMethod(). This works fine.

    However, sometimes, some of these components are deleted using deleteLater(), and these "deleted" components are still returned when findChildren() is called again. Then I suspect this is causing QMetaObject::invokeMethod() to crash (according to debugger, it crashes on this function, even using try/catch(...) block).

    This issue is random because deleteLater() just sometimes takes a long time to remove the components, and in this while, I might have the findChildren() called, and there is no way I see to prevent the crash.

    Any idea how to prevent QMetaObject::invokeMethod() from crashing the application?

    JonBJ 1 Reply Last reply
    0
    • D Developer123

      I query some components from the UI sometimes using findChildren() and call their methods using QMetaObject::invokeMethod(). This works fine.

      However, sometimes, some of these components are deleted using deleteLater(), and these "deleted" components are still returned when findChildren() is called again. Then I suspect this is causing QMetaObject::invokeMethod() to crash (according to debugger, it crashes on this function, even using try/catch(...) block).

      This issue is random because deleteLater() just sometimes takes a long time to remove the components, and in this while, I might have the findChildren() called, and there is no way I see to prevent the crash.

      Any idea how to prevent QMetaObject::invokeMethod() from crashing the application?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Developer123
      Until you get a better answer, mine would be "no". It's up to you not to call things on objects once you have called deleteLater() on them, findChildren() won't check.

      I don't think you/Qt can tell there is a pending deleteLater() on an object. The code for that is simply:

      void QObject::deleteLater()
      {
          QCoreApplication::postEvent(this, new QDeferredDeleteEvent());
      }
      

      If you are in charge of the code which calls deleteLater() on an object in a children tree you might remove the object from the tree yourself at that point to prevent it being found.

      Be aware that whenever Qt deletes a QObject that emits a destroyed() signal. If you really have to cope with the situation you might be able to attach a slot to that which then notes that no further calls must be made on this object, but it's messy/may not work as you need.

      Christian EhrlicherC 1 Reply Last reply
      0
      • JonBJ JonB

        @Developer123
        Until you get a better answer, mine would be "no". It's up to you not to call things on objects once you have called deleteLater() on them, findChildren() won't check.

        I don't think you/Qt can tell there is a pending deleteLater() on an object. The code for that is simply:

        void QObject::deleteLater()
        {
            QCoreApplication::postEvent(this, new QDeferredDeleteEvent());
        }
        

        If you are in charge of the code which calls deleteLater() on an object in a children tree you might remove the object from the tree yourself at that point to prevent it being found.

        Be aware that whenever Qt deletes a QObject that emits a destroyed() signal. If you really have to cope with the situation you might be able to attach a slot to that which then notes that no further calls must be made on this object, but it's messy/may not work as you need.

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        ... or don't use deleteLater() for those objects.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved