diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2020-10-15 12:16:08 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2020-10-15 13:16:57 +0200 |
commit | fa48382c492097d06d7ce066ac36dba04fd5e801 (patch) | |
tree | 6d8bc1d8b0ab829c09554ebe93fb3d6155b29d4b /src/qmlcompiler/qdeferredpointer_p.h | |
parent | 3abe34472b56d32aa1b14f54aa8a0e2ebe4d5537 (diff) |
QDeferredPointer: Remove the factory before executing it
Only this way we avoid infinite recursion on cyclic references.
Change-Id: I4d6323a093d17f7d55965dc40f9dc5f1a647df9c
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qmlcompiler/qdeferredpointer_p.h')
-rw-r--r-- | src/qmlcompiler/qdeferredpointer_p.h | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/src/qmlcompiler/qdeferredpointer_p.h b/src/qmlcompiler/qdeferredpointer_p.h index 07875013fa..b8a06cc13c 100644 --- a/src/qmlcompiler/qdeferredpointer_p.h +++ b/src/qmlcompiler/qdeferredpointer_p.h @@ -50,7 +50,6 @@ class QDeferredFactory public: T create() const; bool isValid() const; - void clear(); }; template<typename T> @@ -79,8 +78,9 @@ public: operator QSharedPointer<T>() const { if (m_factory && m_factory->isValid()) { - const_cast<std::remove_const_t<T> &>(*m_data) = m_factory->create(); - m_factory->clear(); + Factory localFactory; + std::swap(localFactory, *m_factory); // Swap before executing, to avoid recursion + const_cast<std::remove_const_t<T> &>(*m_data) = localFactory.create(); } return m_data; } @@ -135,8 +135,10 @@ public: if (m_factory) { auto factory = m_factory.toStrongRef(); if (factory->isValid()) { - const_cast<std::remove_const_t<T> &>(*(m_data.toStrongRef())) = factory->create(); - factory->clear(); + Factory localFactory; + std::swap(localFactory, *factory); // Swap before executing, to avoid recursion + const_cast<std::remove_const_t<T> &>(*(m_data.toStrongRef())) + = localFactory.create(); } } return m_data; |