Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-135626

QPointer causes unneccessary (invalid) downcasts

    XMLWordPrintable

Details

    • 5
    • 51cd57116 (dev), 6a8b3344c (6.9), e3a80c2dd (6.8), e5f9eb504 (tqtc/lts-6.5)

    Description

      QPointer is used to track whether a QObject is still alive, and so it's unsurprising that checking whether the pointer is null, or equal to some other pointer (raw or Q) is sometimes done in situations where the QObject is already in the process of being destroyed and therefore already demoted from T, the QPointer template argument, to some base class.

      Because of the way we made QPointer SCARY¹, calling data() will cast from QObject* to T*, which is UB if the T was already demoted. A comparison on the raw pointer level would not cast, so would not invoke UB, so it's (our implementation of) QPointer which is introducing this problem.

      It's hard to fix this in the general case, but the various ways to spell isNull() should all be equivalent to isNull() (which does not cause UB, because it doesn't cast), and when we compare against some base X of T, we should do the comparison at the common_type<X*,T*> level, to allow users in those situation to avoid the cast<T> by comparing with a lesser-derived other.

      ¹ Originally 3f7741fbe7ab4140f8f971c0cf88bb04e7feea6b, then changed in
      cc7239da8d1ab95e68e12a64df3ca3051419cb34 and
      351c738fc4586bf354c9363fb78e190bdfca4617.

      Eventually, we may need to rethink how this works, and store an actual T*, and share code by casting that down to QObject* where needed, and not the other ways around.

      But that's Qt 7 talk. We should fix what we can in Qt 6 and make a follow-up task for Qt 7.

      Attachments

        For Gerrit Dashboard: QTBUG-135626
        # Subject Branch Project Status CR V

        Activity

          People

            mmutz Marc Mutz
            mmutz Marc Mutz
            Vladimir Minenko Vladimir Minenko
            Alex Blasche Alex Blasche
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There is 1 open Gerrit change