aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/core/lib/support/reference_counted.cc4
-rw-r--r--src/core/lib/support/reference_counted.h4
-rw-r--r--test/core/support/reference_counted_test.cc7
3 files changed, 12 insertions, 3 deletions
diff --git a/src/core/lib/support/reference_counted.cc b/src/core/lib/support/reference_counted.cc
index 853a29da2f..84ede18efe 100644
--- a/src/core/lib/support/reference_counted.cc
+++ b/src/core/lib/support/reference_counted.cc
@@ -20,6 +20,8 @@
#include <grpc/support/log.h>
+#include "src/core/lib/support/memory.h"
+
namespace grpc_core {
void ReferenceCounted::Ref(const DebugLocation& location, const char* reason) {
@@ -49,7 +51,7 @@ bool ReferenceCounted::Unref(const DebugLocation& location,
bool ReferenceCounted::Unref() {
if (gpr_unref(&refs_)) {
- delete this;
+ Delete(this);
return true;
}
return false;
diff --git a/src/core/lib/support/reference_counted.h b/src/core/lib/support/reference_counted.h
index ebd620c553..e71b4fc593 100644
--- a/src/core/lib/support/reference_counted.h
+++ b/src/core/lib/support/reference_counted.h
@@ -39,6 +39,10 @@ class ReferenceCounted {
ReferenceCounted& operator=(const ReferenceCounted&) = delete;
protected:
+ // Allow Delete() to access destructor.
+ template<typename T>
+ friend void Delete(T*);
+
explicit ReferenceCounted(TraceFlag* trace_flag) : trace_flag_(trace_flag) {
gpr_ref_init(&refs_, 1);
}
diff --git a/test/core/support/reference_counted_test.cc b/test/core/support/reference_counted_test.cc
index 119fc1b600..6613f06585 100644
--- a/test/core/support/reference_counted_test.cc
+++ b/test/core/support/reference_counted_test.cc
@@ -17,7 +17,10 @@
*/
#include "src/core/lib/support/reference_counted.h"
+
#include <gtest/gtest.h>
+
+#include "src/core/lib/support/memory.h"
#include "test/core/util/test_config.h"
namespace grpc_core {
@@ -39,12 +42,12 @@ TEST(ReferenceCounted, StackAllocatedWithExtraRef) {
}
TEST(ReferenceCounted, HeapAllocated) {
- Foo* foo = new Foo();
+ Foo* foo = New<Foo>();
foo->Unref();
}
TEST(ReferenceCounted, HeapAllocatedWithExtraRef) {
- Foo* foo = new Foo();
+ Foo* foo = New<Foo>();
foo->Ref();
foo->Unref();
foo->Unref();