Merge "libbinder_ndk: assert for known bad refcounting"
diff --git a/libs/binder/ndk/include_ndk/android/binder_auto_utils.h b/libs/binder/ndk/include_ndk/android/binder_auto_utils.h
index d947e7b..cc0a29d 100644
--- a/libs/binder/ndk/include_ndk/android/binder_auto_utils.h
+++ b/libs/binder/ndk/include_ndk/android/binder_auto_utils.h
@@ -30,6 +30,8 @@
#include <android/binder_parcel.h>
#include <android/binder_status.h>
+#include <assert.h>
+
#ifdef __cplusplus
#include <cstddef>
@@ -76,7 +78,11 @@
* Takes ownership of one strong refcount of binder
*/
void set(AIBinder* binder) {
- if (mBinder != nullptr) AIBinder_decStrong(mBinder);
+ AIBinder* old = *const_cast<AIBinder* volatile*>(&mBinder);
+ if (old != nullptr) AIBinder_decStrong(old);
+ if (old != *const_cast<AIBinder* volatile*>(&mBinder)) {
+ __assert(__FILE__, __LINE__, "Race detected.");
+ }
mBinder = binder;
}
diff --git a/libs/binder/ndk/include_ndk/android/binder_interface_utils.h b/libs/binder/ndk/include_ndk/android/binder_interface_utils.h
index 5a4196a..1a9018a 100644
--- a/libs/binder/ndk/include_ndk/android/binder_interface_utils.h
+++ b/libs/binder/ndk/include_ndk/android/binder_interface_utils.h
@@ -30,6 +30,8 @@
#include <android/binder_auto_utils.h>
#include <android/binder_ibinder.h>
+#include <assert.h>
+
#ifdef __cplusplus
#include <memory>
@@ -39,11 +41,18 @@
/**
* analog using std::shared_ptr for internally held refcount
+ *
+ * ref must be called at least one time during the lifetime of this object. The recommended way to construct
+ * this object is with SharedRefBase::make.
*/
class SharedRefBase {
public:
SharedRefBase() {}
- virtual ~SharedRefBase() {}
+ virtual ~SharedRefBase() {
+ std::call_once(mFlagThis, [&]() {
+ __assert(__FILE__, __LINE__, "SharedRefBase: no ref created during lifetime");
+ });
+ }
/**
* A shared_ptr must be held to this object when this is called. This must be called once during