protected:
RefBase();
virtual ~RefBase();
-
+
+ // called when the last reference goes away. this is responsible for
+ // calling the destructor. The default implementation just does
+ // "delete this;".
+ // Make sure to never acquire a strong reference from this function. The
+ // same restrictions than for destructors apply.
+ virtual void destroy() const;
+
//! Flags for extendObjectLifetime()
enum {
OBJECT_LIFETIME_WEAK = 0x0001,
const_cast<RefBase*>(this)->onFirstRef();
}
+void RefBase::destroy() const {
+ delete this;
+}
+
void RefBase::decStrong(const void* id) const
{
weakref_impl* const refs = mRefs;
if (c == 1) {
const_cast<RefBase*>(this)->onLastStrongRef(id);
if ((refs->mFlags&OBJECT_LIFETIME_WEAK) != OBJECT_LIFETIME_WEAK) {
- delete this;
+ destroy();
}
}
refs->removeWeakRef(id);
if ((impl->mFlags&OBJECT_LIFETIME_WEAK) != OBJECT_LIFETIME_WEAK) {
if (impl->mStrong == INITIAL_STRONG_VALUE)
- delete impl->mBase;
+ if (impl->mBase)
+ impl->mBase->destroy();
else {
// LOGV("Freeing refs %p of old RefBase %p\n", this, impl->mBase);
delete impl;
} else {
impl->mBase->onLastWeakRef(id);
if ((impl->mFlags&OBJECT_LIFETIME_FOREVER) != OBJECT_LIFETIME_FOREVER) {
- delete impl->mBase;
+ if (impl->mBase)
+ impl->mBase->destroy();
}
}
}