mirror of
https://github.com/AxioDL/boo.git
synced 2025-12-15 16:16:26 +00:00
IObj destructor race condition fix
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#define BOOOBJECT_HPP
|
||||
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
|
||||
namespace boo
|
||||
{
|
||||
@@ -9,13 +10,26 @@ namespace boo
|
||||
class IObj
|
||||
{
|
||||
std::atomic_int m_refCount = {0};
|
||||
protected:
|
||||
std::recursive_mutex* m_mutex = nullptr;
|
||||
public:
|
||||
virtual ~IObj() = default;
|
||||
void increment() { m_refCount++; }
|
||||
void decrement()
|
||||
{
|
||||
if (m_refCount.fetch_sub(1) == 1)
|
||||
delete this;
|
||||
{
|
||||
if (std::recursive_mutex* mutex = m_mutex)
|
||||
{
|
||||
mutex->lock();
|
||||
delete this;
|
||||
mutex->unlock();
|
||||
}
|
||||
else
|
||||
{
|
||||
delete this;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user