diff --git a/libutils/Android.bp b/libutils/Android.bp index 13e4c02de0db..2a83fe953067 100644 --- a/libutils/Android.bp +++ b/libutils/Android.bp @@ -164,6 +164,7 @@ cc_library { srcs: [ "Trace.cpp", ], + cflags: ["-DCHECK_SDK"], }, linux: { header_libs: ["libbase_headers"], diff --git a/libutils/RefBase.cpp b/libutils/RefBase.cpp index b57e28741de2..94ac86b9a99d 100644 --- a/libutils/RefBase.cpp +++ b/libutils/RefBase.cpp @@ -751,16 +751,26 @@ RefBase::~RefBase() } } else if (mRefs->mStrong.load(std::memory_order_relaxed) == INITIAL_STRONG_VALUE) { // We never acquired a strong reference on this object. +#ifdef CHECK_SDK +#include + // In Android Q this also fails for objects with 0 weak references. + // Restore old behavior if the SDK version precedes Q. + if ((android_get_application_target_sdk_version() >= __ANDROID_API_Q__) || (mRefs->mWeak.load() != 0)) { +#endif #if DEBUG_REFBASE_DESTRUCTION - // Treating this as fatal is prone to causing boot loops. For debugging, it's - // better to treat as non-fatal. - ALOGD("RefBase: Explicit destruction, weak count = %d (in %p)", mRefs->mWeak.load(), this); + // Treating this as fatal is prone to causing boot loops. For debugging, it's + // better to treat as non-fatal. + ALOGD("RefBase: Explicit destruction, weak count = %d (in %p)", mRefs->mWeak.load(), this); #if CALLSTACK_ENABLED - CallStack::logStack(LOG_TAG); + CallStack::logStack(LOG_TAG); #endif #else - LOG_ALWAYS_FATAL("RefBase: Explicit destruction, weak count = %d", mRefs->mWeak.load()); + LOG_ALWAYS_FATAL("RefBase: Explicit destruction, weak count = %d", mRefs->mWeak.load()); +#endif +#ifdef CHECK_SDK + } + delete mRefs; #endif } // For debugging purposes, clear mRefs. Ineffective against outstanding wp's.