Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Updating string property in qml using setProperty in cpp is throwing segmentation fault while running for two to three minutes

Updating string property in qml using setProperty in cpp is throwing segmentation fault while running for two to three minutes

Scheduled Pinned Locked Moved Solved Mobile and Embedded
9 Posts 4 Posters 581 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.
  • R Offline
    R Offline
    rvignesh496
    wrote on 20 Feb 2024, 10:19 last edited by
    #1

    Hi all,

    QMake version 3.1
    Using Qt version 5.12.8 in /usr/lib/aarch64-linux-gnu

    I'm currently using open source version of qt available for arm, above is the installed version. My application requires me to update a string from cpp to qml. But the setProperty part of the code is throwing segmentation fault while the application is kept on a run for a period of time. setProperty for int and real doesn't have any issue and I am sure that the input data to the setProperty is correct.

    cpp code which runs in a loop (there are other properties updated similarly as well)

    QString gear = QString::fromStdString(payloadJson["gear"].asString());
     valueSource->setProperty("gear", gear);
    

    qml code for the property

    property string gear: "N"
    

    I doubt if there any issue in this particular qt version and its associated libraries, if so, how can update the dependent libraries.

    Thank you in advance for any help.
    Vignesh R

    S J 2 Replies Last reply 20 Feb 2024, 10:22
    0
    • R rvignesh496
      20 Feb 2024, 10:19

      Hi all,

      QMake version 3.1
      Using Qt version 5.12.8 in /usr/lib/aarch64-linux-gnu

      I'm currently using open source version of qt available for arm, above is the installed version. My application requires me to update a string from cpp to qml. But the setProperty part of the code is throwing segmentation fault while the application is kept on a run for a period of time. setProperty for int and real doesn't have any issue and I am sure that the input data to the setProperty is correct.

      cpp code which runs in a loop (there are other properties updated similarly as well)

      QString gear = QString::fromStdString(payloadJson["gear"].asString());
       valueSource->setProperty("gear", gear);
      

      qml code for the property

      property string gear: "N"
      

      I doubt if there any issue in this particular qt version and its associated libraries, if so, how can update the dependent libraries.

      Thank you in advance for any help.
      Vignesh R

      S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 20 Feb 2024, 10:22 last edited by
      #2

      @rvignesh496 are you sure valueSource is not a dangling pointer?

      (Z(:^

      R 1 Reply Last reply 21 Feb 2024, 05:43
      1
      • R rvignesh496
        20 Feb 2024, 10:19

        Hi all,

        QMake version 3.1
        Using Qt version 5.12.8 in /usr/lib/aarch64-linux-gnu

        I'm currently using open source version of qt available for arm, above is the installed version. My application requires me to update a string from cpp to qml. But the setProperty part of the code is throwing segmentation fault while the application is kept on a run for a period of time. setProperty for int and real doesn't have any issue and I am sure that the input data to the setProperty is correct.

        cpp code which runs in a loop (there are other properties updated similarly as well)

        QString gear = QString::fromStdString(payloadJson["gear"].asString());
         valueSource->setProperty("gear", gear);
        

        qml code for the property

        property string gear: "N"
        

        I doubt if there any issue in this particular qt version and its associated libraries, if so, how can update the dependent libraries.

        Thank you in advance for any help.
        Vignesh R

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 20 Feb 2024, 10:25 last edited by
        #3

        @rvignesh496 is your class instance properly parented / assigned ? If not the QML engine takes ownership and it might have been deleted by the GC


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        R 1 Reply Last reply 21 Feb 2024, 05:53
        1
        • S sierdzio
          20 Feb 2024, 10:22

          @rvignesh496 are you sure valueSource is not a dangling pointer?

          R Offline
          R Offline
          rvignesh496
          wrote on 21 Feb 2024, 05:43 last edited by
          #4

          @sierdzio

          yes valueSource is assigned with the qml object before any setProperty is called. When I comment out any update to string property, but leave the updates to int and real intact the program runs without any issues for a longer period of time.

          1 Reply Last reply
          0
          • J J.Hilk
            20 Feb 2024, 10:25

            @rvignesh496 is your class instance properly parented / assigned ? If not the QML engine takes ownership and it might have been deleted by the GC

            R Offline
            R Offline
            rvignesh496
            wrote on 21 Feb 2024, 05:53 last edited by
            #5

            @J-Hilk

            This is the initialization in main function before any setproperty is called.

            valueSource = rootObject->findChild<QObject*>("valueSource");
            

            All the properties are defined in a seperate qml file, shown as below

            Item {
                id: valueSource
                objectName: "valueSource"
                property string ...
            

            The valueSource object is used in the main.qml file, as below

            ValueSource {
                    id: valueSource
                }
            

            Am I missing something or doing any intializations wrong?

            Thanks in advance for any help.

            S J 2 Replies Last reply 21 Feb 2024, 07:23
            0
            • R rvignesh496
              21 Feb 2024, 05:53

              @J-Hilk

              This is the initialization in main function before any setproperty is called.

              valueSource = rootObject->findChild<QObject*>("valueSource");
              

              All the properties are defined in a seperate qml file, shown as below

              Item {
                  id: valueSource
                  objectName: "valueSource"
                  property string ...
              

              The valueSource object is used in the main.qml file, as below

              ValueSource {
                      id: valueSource
                  }
              

              Am I missing something or doing any intializations wrong?

              Thanks in advance for any help.

              S Offline
              S Offline
              sierdzio
              Moderators
              wrote on 21 Feb 2024, 07:23 last edited by sierdzio
              #6

              @rvignesh496 maybe it is a Qt bug after all? You could upgrade to at least latest version in your branch (5.12.12).

              And just to be on the safe side, do this:

              if (QPointer<QObject> pointer(valueSource); pointer.isNull() == false) {
                const auto gear = QString::fromStdString(payloadJson["gear"].asString());
                pointer->setProperty("gear", gear);
              } else {
                qCritical() << "ERROR: invalid pointer" << valueSource;
              }
              

              (Z(:^

              1 Reply Last reply
              1
              • R rvignesh496
                21 Feb 2024, 05:53

                @J-Hilk

                This is the initialization in main function before any setproperty is called.

                valueSource = rootObject->findChild<QObject*>("valueSource");
                

                All the properties are defined in a seperate qml file, shown as below

                Item {
                    id: valueSource
                    objectName: "valueSource"
                    property string ...
                

                The valueSource object is used in the main.qml file, as below

                ValueSource {
                        id: valueSource
                    }
                

                Am I missing something or doing any intializations wrong?

                Thanks in advance for any help.

                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 21 Feb 2024, 07:35 last edited by
                #7

                @rvignesh496 ok, you're doing it the other way around, which is the less popular, discouraged way to do it :D

                How sure are you, that your valueSource item is staying alive ?
                I would add a console.log to the destruction signal

                Component.onDestruction: console.log("Destruction Beginning!")
                

                and eventually set a breakpoint there.

                Because your cpp pointer will become invalid if your QML component gets cleaned up and you will not be notified, usually.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  rvignesh496
                  wrote on 22 Feb 2024, 06:00 last edited by
                  #8
                  function update_property(msg) {
                      property = msg;
                  }
                  

                  Had a function similar to the above implemented in qml and from cpp I'm calling the function to update the string.

                  QMetaObject::invokeMethod(valueSource, "update_propertyr", Q_ARG(QVariant, QVariant(property)));
                  

                  This solved the issue, and I'm not running into any segmentation fault even during long run.

                  TomZT 1 Reply Last reply 22 Feb 2024, 10:35
                  0
                  • R rvignesh496 has marked this topic as solved on 22 Feb 2024, 06:34
                  • R rvignesh496
                    22 Feb 2024, 06:00
                    function update_property(msg) {
                        property = msg;
                    }
                    

                    Had a function similar to the above implemented in qml and from cpp I'm calling the function to update the string.

                    QMetaObject::invokeMethod(valueSource, "update_propertyr", Q_ARG(QVariant, QVariant(property)));
                    

                    This solved the issue, and I'm not running into any segmentation fault even during long run.

                    TomZT Offline
                    TomZT Offline
                    TomZ
                    wrote on 22 Feb 2024, 10:35 last edited by
                    #9

                    @rvignesh496 please note that many parts of Qt want you to be aware which thread you are in.

                    At reading of the initial post I already suspected that this is due to you calling the cpp setProperty from a thread that is not the Qt Gui thread (also the 'main' thread).

                    The code you pasted in the end is strong indication that this is the case, indeed. As that is a typical way to avoid threading issues.

                    Bottom line, you want to do changes to your Qt objects that are used by QML ONLY in the main Qt thread.

                    Any calculations can be done elsewhere, just make sure you never read or write a property (or their backing data) from another thread.

                    1 Reply Last reply
                    0

                    1/9

                    20 Feb 2024, 10:19

                    • Login

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