android C++ weakref_impl对象标志位的作用
当mFlags 为OBJECT_LIFETIME_STRONG 时,强引用计数为0时,销毁对象
当mFlags为OBJECT_LIFETIME_WEAK时,强引用计数为0时,不销毁对象,弱引用减为0时,才销毁对象,由于弱引用计数 >= 强引用计数,所以OBJECT_LIFETIME_WEAK延长了对象的存在时间,下面的代码说明了这种情况。当mWeak == 0 且 mFlags == OBJECT_LIFETIME_WEAK时,释放目标对象。
void RefBase::weakref_type::decWeak(const void* id)
{
const int32_t c = android_atomic_dec(&impl->mWeak);
if (c != 1) return;
if ((impl->mFlags&OBJECT_LIFETIME_WEAK) == OBJECT_LIFETIME_STRONG) {
…
} else {
// less common case: lifetime is OBJECT_LIFETIME_{WEAK|FOREVER}
impl->mBase->onLastWeakRef(id);
if ((impl->mFlags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_WEAK) {
// this is the OBJECT_LIFETIME_WEAK case. The last weak-reference
// is gone, we can destroy the object.
delete impl->mBase;
}
}
}java-javascript 风之境地