From dbded811329669ee62023c1da9d28b297b6f233f Mon Sep 17 00:00:00 2001 From: Wehrwolfmann <256216494+wehrwolfmann@users.noreply.github.com> Date: Sat, 19 Sep 2026 20:43:32 +0200 Subject: [PATCH] keep default theme objects alive for the process lifetime The default color/size/page objects live in a Q_GLOBAL_STATIC and are handed out to QML by MProp. When static destruction runs while a QQmlEngine is still alive, they are destroyed first and every binding on MD.MProp.color / MD.Token.color re-evaluates against a destroyed QObject, printing "TypeError: Cannot read property '...' of null". Create the defaults once and keep them for the whole process lifetime. Nothing changes while the engine is up, and an explicitly assigned MdColorMgr is still owned by the Theme it is set on. --- src/style/theme.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/style/theme.cpp b/src/style/theme.cpp index b30c86d7..36f50d9e 100644 --- a/src/style/theme.cpp +++ b/src/style/theme.cpp @@ -80,7 +80,13 @@ struct GlobalTheme { PageContext page_; PageContext* page { &page_ }; }; -Q_GLOBAL_STATIC(GlobalTheme, theGlobalTheme) +// The default objects below are handed to QML and bindings may still read them +// while static destruction is running, which would turn every color into null. +// Create them once and keep them for the process lifetime. +GlobalTheme* theGlobalTheme() { + static GlobalTheme* const the = new GlobalTheme(); + return the; +} } // namespace @@ -93,11 +99,11 @@ Theme* Theme::qmlAttachedProperties(QObject* object) { return new Theme(object); #define IMPL_ATTACH_PROP(_type_, _name_, _prop_, ...) \ Theme::AttachProp<_type_>& Theme::get_##_name_() { return _prop_; } \ - _type_ Theme::_name_() const { return _prop_.value.value_or(theGlobalTheme->_name_); } \ + _type_ Theme::_name_() const { return _prop_.value.value_or(theGlobalTheme()->_name_); } \ void Theme::set_##_name_(_type_ v) { set_prop(this, v, &Theme::get_##_name_); } \ void Theme::reset_##_name_() { \ - Self* obj = qobject_cast(attachedParent()); \ - reset_prop(this, &Theme::get_##_name_, obj ? obj->_name_() : theGlobalTheme->_name_); \ + Self* obj = qobject_cast(attachedParent()); \ + reset_prop(this, &Theme::get_##_name_, obj ? obj->_name_() : theGlobalTheme()->_name_); \ } IMPL_ATTACH_PROP(QColor, textColor, m_textColor)