From 9971c1c5cefa987778fed770bdb8caa5e444c3d2 Mon Sep 17 00:00:00 2001 From: David Faure Date: Mon, 21 Oct 2024 13:41:51 +0200 Subject: [PATCH] fix: fix crash with Qt 6.8: metaObject() should never be nullptr It was null until ready() was called, but before that, any qobject_cast on the object would crash. In Qt 6.8 QAccessibleWidget looks for "who has widget X as buddy" by iterating over all children() of the parent and doing qobject_cast, hence this new crash. --- src/core/scriptdialogitem_p.cpp | 7 ++++--- src/core/scriptdialogitem_p.h | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/core/scriptdialogitem_p.cpp b/src/core/scriptdialogitem_p.cpp index b27ec109..18f9cb58 100644 --- a/src/core/scriptdialogitem_p.cpp +++ b/src/core/scriptdialogitem_p.cpp @@ -17,19 +17,20 @@ namespace Core { DynamicObject::~DynamicObject() { - free(m_metaObject); + if (m_metaObject != &QObject::staticMetaObject) + free(const_cast(m_metaObject)); } void DynamicObject::addProperty(const QByteArray &name, const QByteArray &type, QMetaType::Type typeId, const QVariant &value) { - Q_ASSERT_X(m_metaObject == nullptr, "addProperty", "Can't add property after calling ready()"); + Q_ASSERT_X(m_metaObject == &QObject::staticMetaObject, "addProperty", "Can't add property after calling ready()"); m_properties.emplace_back(DynamicProperty {name, type, typeId, value}); } void DynamicObject::ready() { - Q_ASSERT_X(m_metaObject == nullptr, "ready", "ready() should be called only once."); + Q_ASSERT_X(m_metaObject == &QObject::staticMetaObject, "ready", "ready() should be called only once."); QMetaObjectBuilder builder; builder.setSuperClass(&QObject::staticMetaObject); diff --git a/src/core/scriptdialogitem_p.h b/src/core/scriptdialogitem_p.h index bca8f137..9a192aaa 100644 --- a/src/core/scriptdialogitem_p.h +++ b/src/core/scriptdialogitem_p.h @@ -56,7 +56,7 @@ class DynamicObject : public QObject QVariant variant; }; - QMetaObject *m_metaObject = nullptr; + const QMetaObject *m_metaObject = &QObject::staticMetaObject; std::vector m_properties; DataChangedFunc m_dataChangedCallback; };