Skip to content

Commit

Permalink
fix: fix crash with Qt 6.8: metaObject() should never be nullptr
Browse files Browse the repository at this point in the history
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<QLabel *>, hence this new crash.
  • Loading branch information
dfaure-kdab authored and narnaud committed Oct 21, 2024
1 parent 1d20208 commit 9971c1c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/core/scriptdialogitem_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ namespace Core {

DynamicObject::~DynamicObject()
{
free(m_metaObject);
if (m_metaObject != &QObject::staticMetaObject)
free(const_cast<QMetaObject *>(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);
Expand Down
2 changes: 1 addition & 1 deletion src/core/scriptdialogitem_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class DynamicObject : public QObject
QVariant variant;
};

QMetaObject *m_metaObject = nullptr;
const QMetaObject *m_metaObject = &QObject::staticMetaObject;
std::vector<DynamicProperty> m_properties;
DataChangedFunc m_dataChangedCallback;
};
Expand Down

0 comments on commit 9971c1c

Please sign in to comment.