[vlc-commits] [Git][videolan/vlc][master] qt: do not call `qmlClearTypeRegistrations()` when there are qml engines existing

Jean-Baptiste Kempf (@jbk) gitlab at videolan.org
Sun Mar 22 22:24:46 UTC 2026



Jean-Baptiste Kempf pushed to branch master at VideoLAN / VLC


Commits:
643b5a27 by Fatih Uzunoglu at 2026-03-22T22:22:43+01:00
qt: do not call `qmlClearTypeRegistrations()` when there are qml engines existing

As per the Qt 6.10 documentation, calling `qmlClearTypeRegistrations()` when there
are QML engines existing is an undefined behavior.

- - - - -


2 changed files:

- modules/gui/qt/maininterface/mainui.cpp
- modules/gui/qt/maininterface/mainui.hpp


Changes:

=====================================
modules/gui/qt/maininterface/mainui.cpp
=====================================
@@ -82,6 +82,8 @@
 
 #include <QScreen>
 
+static bool g_qmlTypesAreRegistered = false;
+
 using  namespace vlc::playlist;
 
 MainUI::MainUI(qt_intf_t *p_intf, MainCtx *mainCtx, QWindow* interfaceWindow,  QObject *parent)
@@ -119,11 +121,25 @@ MainUI::MainUI(qt_intf_t *p_intf, MainCtx *mainCtx, QWindow* interfaceWindow,  Q
 
 MainUI::~MainUI()
 {
-    qmlClearTypeRegistrations();
+    if (!m_engineBound)
+    {
+        // If the global engine is dead, we assume no other QML engine exists hence can clear the
+        // types. Note that there may be multiple QML engines co-existing, but the global engine
+        // is expected to outlive the others.
+        clearQMLTypes();
+    }
+    else
+    {
+        // `QObject::destroyed()` is signalled just before the object dies, but subclass `QQmlEngine`
+        // itself should be dead at that point anyway. This should prevent undefined behavior.
+        connect(m_engineBound, &QObject::destroyed, this, &MainUI::clearQMLTypes, Qt::UniqueConnection);
+    }
 }
 
 bool MainUI::setup(QQmlEngine* engine)
 {
+    m_engineBound = engine;
+
     if (m_mainCtx->hasMediaLibrary())
     {
         engine->addImageProvider(MLCustomCover::providerId, new MLCustomCover(m_mainCtx->getMediaLibrary()));
@@ -186,6 +202,8 @@ QQuickItem* MainUI::createRootItem()
 
 void MainUI::registerQMLTypes()
 {
+    assert(!g_qmlTypesAreRegistered);
+
     {
         const char* uri = "VLC.MainInterface";
         const int versionMajor = 1;
@@ -436,4 +454,12 @@ void MainUI::registerQMLTypes()
         qmlRegisterModule(uri, versionMajor, versionMinor);
         qmlProtectModule(uri, versionMajor);
     }
+
+    g_qmlTypesAreRegistered = true;
+}
+
+void MainUI::clearQMLTypes()
+{
+    qmlClearTypeRegistrations();
+    g_qmlTypesAreRegistered = false;
 }


=====================================
modules/gui/qt/maininterface/mainui.hpp
=====================================
@@ -30,6 +30,7 @@ public:
 
 private:
     void registerQMLTypes();
+    /*static*/ void clearQMLTypes();
     static QObject* getMainCtxInstance(QQmlEngine *, QJSEngine *);
 
     qt_intf_t* m_intf = nullptr;
@@ -38,6 +39,8 @@ private:
 
     QQmlComponent* m_component = nullptr;
     QQuickItem* m_rootItem = nullptr;
+
+    QPointer<QQmlEngine> m_engineBound;
 };
 
 #endif // MAINUI_HPP



View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/643b5a278f40a899d0c981499da70a0721e1dff5

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/643b5a278f40a899d0c981499da70a0721e1dff5
You're receiving this email because of your account on code.videolan.org.




More information about the vlc-commits mailing list