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

Don't detach QVariant in setProperty()/QMP::write()

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4: Low P4: Low
    • None
    • 5.15, 6.5.0
    • Core: Object Model
    • None
    • All

      QMetaProperty::write (and by extension, QObject::setProperty) perform a detach of the passed in QVariant value. The argument to QMetaProperty::write is a const QVariant&, and the documentation gives no warnings of anything funny going on.  The setter of the property we're trying to write takes a const CustomType& so there is no reason to suspect a detach of the QVariant is going to happen. 

      But, in QMetaProperty::write, first a copy of the value is taken. This is still shallow. But a little bit later, where the value is stuffed into a Q_ARG in order to call the setter via QMetaCall, QVariant::data is called on the non-const copy of the value. And as ::data isn't const, we get a detach and thus a deep copy of the value there.

      As a user, I did not expect this API to cause a detach. There is no warning about it in the documentation, and it takes a const QVariant&, and the setter is also using const ref. Perhaps constData() could be used instead of data()? 

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            peppe Giuseppe D'Angelo
            mmutz Marc Mutz
            Vladimir Minenko Vladimir Minenko
            Alex Blasche Alex Blasche
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes