IObj destructor race condition fix

This commit is contained in:
Jack Andersen
2017-11-07 21:33:10 -10:00
parent d04c19a258
commit 1a2fc1d2a3
8 changed files with 31 additions and 15 deletions

View File

@@ -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;
}
}
}
};