diff options
author | Jason Sams <jsams@google.com> | 2015-02-19 17:19:52 -0800 |
---|---|---|
committer | Jason Sams <jsams@google.com> | 2015-02-19 17:19:52 -0800 |
commit | d22a6f09dc4df8fd19b85bace5085aa099b90e53 (patch) | |
tree | f89796b0a2c5f8872f4c409f09dd1475ca242df8 /rs/java | |
parent | e460f88d37ce035718987b9424190956a6bba52f (diff) | |
download | frameworks_base-d22a6f09dc4df8fd19b85bace5085aa099b90e53.zip frameworks_base-d22a6f09dc4df8fd19b85bace5085aa099b90e53.tar.gz frameworks_base-d22a6f09dc4df8fd19b85bace5085aa099b90e53.tar.bz2 |
Implement finalizer for RS contexts.
Fixes memory leak when apps forget to call .destroy() on the context.
Change-Id: Ida4685768e92cfe3875da38846d17b86cc386cd0
Diffstat (limited to 'rs/java')
-rw-r--r-- | rs/java/android/renderscript/RenderScript.java | 48 |
1 files changed, 35 insertions, 13 deletions
diff --git a/rs/java/android/renderscript/RenderScript.java b/rs/java/android/renderscript/RenderScript.java index e7487aa..417bfe2 100644 --- a/rs/java/android/renderscript/RenderScript.java +++ b/rs/java/android/renderscript/RenderScript.java @@ -938,6 +938,8 @@ public class RenderScript { long mDev; long mContext; + private boolean mDestroyed = false; + @SuppressWarnings({"FieldCanBeLocal"}) MessageThread mMessageThread; @@ -1382,6 +1384,38 @@ public class RenderScript { nContextFinish(); } + private void helpDestroy() { + boolean shouldDestroy = false; + synchronized(this) { + if (!mDestroyed) { + shouldDestroy = true; + mDestroyed = true; + } + } + + if (shouldDestroy) { + nContextFinish(); + + nContextDeinitToClient(mContext); + mMessageThread.mRun = false; + try { + mMessageThread.join(); + } catch(InterruptedException e) { + } + + nContextDestroy(); + + nDeviceDestroy(mDev); + mDev = 0; + } + } + + protected void finalize() throws Throwable { + helpDestroy(); + super.finalize(); + } + + /** * Destroys this RenderScript context. Once this function is called, * using this context or any objects belonging to this context is @@ -1390,19 +1424,7 @@ public class RenderScript { */ public void destroy() { validate(); - nContextFinish(); - - nContextDeinitToClient(mContext); - mMessageThread.mRun = false; - try { - mMessageThread.join(); - } catch(InterruptedException e) { - } - - nContextDestroy(); - - nDeviceDestroy(mDev); - mDev = 0; + helpDestroy(); } boolean isAlive() { |