libluna/SharedPtr: Count references with separately created objects properly

This commit is contained in:
apio 2023-08-03 10:32:22 +02:00
parent 5aa042a5f2
commit d41fb85466
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -23,7 +23,7 @@ struct Shareable
return m_ref_count == 0; return m_ref_count == 0;
} }
Atomic<int> m_ref_count { 1 }; Atomic<int> m_ref_count { 0 };
}; };
template <typename T> class SharedPtr template <typename T> class SharedPtr
@ -36,6 +36,7 @@ template <typename T> class SharedPtr
SharedPtr(T* ptr) : m_ptr(ptr) SharedPtr(T* ptr) : m_ptr(ptr)
{ {
if (m_ptr) shareable()->ref();
} }
SharedPtr(const SharedPtr<T>& other) : m_ptr(other.m_ptr) SharedPtr(const SharedPtr<T>& other) : m_ptr(other.m_ptr)
@ -50,7 +51,6 @@ template <typename T> class SharedPtr
template <typename Tp> operator SharedPtr<Tp>() template <typename Tp> operator SharedPtr<Tp>()
{ {
if (m_ptr) shareable()->ref();
return { (Tp*)m_ptr }; return { (Tp*)m_ptr };
} }