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

Regression: Making changes to a QML singleton results in arbitrary property value and type corruption

    XMLWordPrintable

Details

    Description

      I was optimizing binding expressions by exposing some often duplicated values as reusable properties, when I noticed those entirely cosmetic changes have wrecked my entire project.

      I had a hard time reproducing it, but finally succeeded. Here is the initial code, producing the expected result as seen in the first image:

      // S_Settings.qml
      pragma Singleton
      import QtQuick 2.7
      
      Item {
        property real bsize: 50
      }
      
      // in main()
      qmlRegisterSingletonType(QUrl(QStringLiteral("qrc:/S_Settings.qml")), "Set", 1, 0, "SS");
      
      // main.qml
      import QtQuick 2.7
      import QtQuick.Window 2.2
      import Set 1.0
      
      Window {
        id: main
        visible: true
        width: 300
        height: 200
      
        Rectangle {
          anchors.centerIn: parent
          width: SS.bsize * 4
          height: SS.bsize * 2
          radius: SS.bsize * .5
          color: "red"
          border.color: "black"
          border.width: SS.bsize * .1
        }
      

      Now, let's make some changes, and introduce two more derived properties in the singleton and use them in main.cpp

      // S_Settings.qml
      pragma Singleton
      import QtQuick 2.7
      
      Item {
        property real bsize: 50
        property real tenth: bsize * .1
        property real half: bsize * .5
      }
      
      // main.qml
      import QtQuick 2.7
      import QtQuick.Window 2.2
      import Set 1.0
      
      Window {
        id: main
        visible: true
        width: 300
        height: 200
      
        Rectangle {
          anchors.centerIn: parent
          width: SS.bsize * 4
          height: SS.bsize * 2
          radius: SS.half
          color: "red"
          border.color: "black"
          border.width: SS.tenth
        }
      }
      

      At a first glance, everything is OK, in this particular and trivial example the changes still produce the same output as the original code.

      But now let's simply change the order of the properties:

      // S_Settings.qml
      pragma Singleton
      import QtQuick 2.7
      
      Item {
        property real bsize: 50
        property real half: bsize * .5
        property real tenth: bsize * .1
      }
      

      The output is already corrupted, as evident from the second image. The rectangle is incorrectly rounded and the border is abnormally thick.

      Let's push that property another line up:

      // S_Settings.qml
      pragma Singleton
      import QtQuick 2.7
      
      Item {
        property real half: bsize * .5
        property real bsize: 50
        property real tenth: bsize * .1
      }
      

      Another cosmetic change that outta have no effect whatsoever and now the output is even bugger. Now all that we have is a small black rectangle.

      Note that in this small example it only messes up property values. In my actual production code the effect was devastating. Aside from messed up property values, property types went crazy as well, the engine saw colors as ints, strings as bools, ints as nulls and so on. The effect ranged from entirely messed up UI with objects all in the wrong places, wrong sizes and wrong colors, through absolutely nothing showing up, and all the way to crashes, depending on simple changes of the order of properties in the singleton. Also, unlike in this example, in my production code I immediately got some visual artifacts, but simple reordering of properties made it even worse.

      UPDATE:

      It seems to be qml cache related, as purging the cache for that application solves the issue. Unfortunately, it is not as simple to do that on a user system. I can totally see this messing up user applications on updates, unless every QML file has changed in order to force recompilation, the end user application will succumb to this bug.

      Attachments

        1. bug1.png
          bug1.png
          2 kB
        2. bug2.png
          bug2.png
          0.7 kB
        3. bug3.png
          bug3.png
          0.7 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            shausman Simon Hausmann
            dgo dgo
            Votes:
            4 Vote for this issue
            Watchers:
            13 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes