Updating string property in qml using setProperty in cpp is throwing segmentation fault while running for two to three minutes
-
wrote on 20 Feb 2024, 10:19 last edited by
Hi all,
QMake version 3.1
Using Qt version 5.12.8 in /usr/lib/aarch64-linux-gnuI'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 -
Hi all,
QMake version 3.1
Using Qt version 5.12.8 in /usr/lib/aarch64-linux-gnuI'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@rvignesh496 are you sure
valueSource
is not a dangling pointer? -
Hi all,
QMake version 3.1
Using Qt version 5.12.8 in /usr/lib/aarch64-linux-gnuI'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@rvignesh496 is your class instance properly parented / assigned ? If not the QML engine takes ownership and it might have been deleted by the GC
-
@rvignesh496 are you sure
valueSource
is not a dangling pointer?wrote on 21 Feb 2024, 05:43 last edited byyes 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.
-
@rvignesh496 is your class instance properly parented / assigned ? If not the QML engine takes ownership and it might have been deleted by the GC
wrote on 21 Feb 2024, 05:53 last edited by@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-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.
@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; }
-
@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.
@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 signalComponent.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.
-
wrote on 22 Feb 2024, 06:00 last edited by
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.
-
-
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.
wrote on 22 Feb 2024, 10:35 last edited by@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/9