WakeLock finalized while still held WakeLock finalized while still held android android

WakeLock finalized while still held


Ok I believe I found the problem.

The WakeLock is reference counted. That means that if a second acquire()happens it will just bump the reference count. Every call to acquire()needs to be protected by a call to isHeld() as in:

if ((keepScreenOn != null) &&           // we have a WakeLock    (keepScreenOn.isHeld() == false)) {  // but we don't hold it   keepScreenOn.acquire();}

I had assumed that acquire() on a lock I held did nothing somultiple acquire() calls caused the problem. Since the referencecount is not zero the GC throws an error.


I know this question is old, but keep in mind that WakeLocks are 'reference counted' by default. You can turn off reference counting using setReferenceCounted(boolean), see http://developer.android.com/reference/android/os/PowerManager.WakeLock.html#setReferenceCounted(boolean)


No, there is only one declaration at the global scope and allcalls to the acquire() and release() occur in that scope. Iprintln when they happen and the acquire() occurs once and therelease occurs once.