1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 18:54:02 +01:00

[llvm] Add asserts in (ThreadSafe)?RefCountedBase destructors

Added a trivial destructor in release mode and in debug mode a destructor that asserts RefCount is indeed zero.
This ensure people aren't manually (maybe accidentally) destroying these objects like in this contrived example.
```lang=c++
{
  std::unique_ptr<SomethingRefCounted> Object;
  holdIntrusiveOwnership(Object.get());
  // Object Destructor called here will assert.
}
```

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D92480
This commit is contained in:
Nathan James 2020-12-07 20:20:08 +00:00
parent 24b9266a7e
commit d6790a9df8

View File

@ -75,6 +75,18 @@ public:
RefCountedBase(const RefCountedBase &) {}
RefCountedBase &operator=(const RefCountedBase &) = delete;
#ifndef NDEBUG
~RefCountedBase() {
assert(RefCount == 0 &&
"Destruction occured when there are still references to this.");
}
#else
// Default the destructor in release builds, A trivial destructor may enable
// better codegen.
~RefCountedBase() = default;
#endif
public:
void Retain() const { ++RefCount; }
void Release() const {
@ -94,6 +106,17 @@ protected:
ThreadSafeRefCountedBase &
operator=(const ThreadSafeRefCountedBase &) = delete;
#ifndef NDEBUG
~ThreadSafeRefCountedBase() {
assert(RefCount == 0 &&
"Destruction occured when there are still references to this.");
}
#else
// Default the destructor in release builds, A trivial destructor may enable
// better codegen.
~ThreadSafeRefCountedBase() = default;
#endif
public:
void Retain() const { RefCount.fetch_add(1, std::memory_order_relaxed); }