From 6fe248139223a9dfaab709bc13849bdc16f27564 Mon Sep 17 00:00:00 2001 From: Mathias Agopian Date: Wed, 10 Aug 2011 21:07:02 -0700 Subject: fix a memory leak and memory corruption in RefBase we would leak a weakref_impl if a RefBase was never incWeak()'ed. there was also a dangling pointer that would cause memory corruption and double-delete when a custom destroyer was used to delay the execution of ~RefBase. it turns out that the custom destroyer feature caused most of the problems, so it's now gone. The only client was SurfaceFlinger who now handles things on its own. RefBase is essentially back its "gingerbread" state, but the code was slightly cleaned-up. Bug: 5151207, 5084978 Change-Id: Id6ef1d707f96d96366f75068f77b30e0ce2722a5 --- include/utils/RefBase.h | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) (limited to 'include/utils/RefBase.h') diff --git a/include/utils/RefBase.h b/include/utils/RefBase.h index ca170827f1b2..c7a9b7895970 100644 --- a/include/utils/RefBase.h +++ b/include/utils/RefBase.h @@ -80,9 +80,12 @@ public: void incWeak(const void* id); void decWeak(const void* id); + // acquires a strong reference if there is already one. bool attemptIncStrong(const void* id); - //! This is only safe if you have set OBJECT_LIFETIME_FOREVER. + // acquires a weak reference if there is already one. + // This is not always safe. see ProcessState.cpp and BpBinder.cpp + // for proper use. bool attemptIncWeak(const void* id); //! DEBUGGING ONLY: Get current weak ref count. @@ -116,28 +119,15 @@ public: typedef RefBase basetype; - // used to override the RefBase destruction. - class Destroyer { - friend class RefBase; - friend class weakref_type; - public: - virtual ~Destroyer(); - private: - virtual void destroy(RefBase const* base) = 0; - }; - - // Make sure to never acquire a strong reference from this function. The - // same restrictions than for destructors apply. - void setDestroyer(Destroyer* destroyer); - protected: RefBase(); virtual ~RefBase(); //! Flags for extendObjectLifetime() enum { + OBJECT_LIFETIME_STRONG = 0x0000, OBJECT_LIFETIME_WEAK = 0x0001, - OBJECT_LIFETIME_FOREVER = 0x0003 + OBJECT_LIFETIME_MASK = 0x0001 }; void extendObjectLifetime(int32_t mode); @@ -163,7 +153,7 @@ private: RefBase(const RefBase& o); RefBase& operator=(const RefBase& o); - + weakref_impl* const mRefs; }; -- cgit v1.2.3-59-g8ed1b