diff options
author | Miao Wang <miaowang@google.com> | 2016-04-22 14:43:50 -0700 |
---|---|---|
committer | Miao Wang <miaowang@google.com> | 2016-05-11 12:13:34 -0700 |
commit | 5bab643b33fc853808cbcd74d8dde97e269ae96a (patch) | |
tree | 99f5dd4ef1d5b2bd6a0342d03a8c80f19abd75f0 /support | |
parent | 4f44acb8ed103c292258bc1fbfb62efac0db5d9b (diff) | |
download | rs-5bab643b33fc853808cbcd74d8dde97e269ae96a.tar.gz |
[RenderScript] Add finalizer to support lib context.
Bug: 28347289
- Adding finalizer to support lib context, avoiding leak of native
memory.
Change-Id: Ica3fdbfe65c0c651877be4324d0cb95a7256f8d7
(cherry picked from commit c90157d08c02c2c255580485346fdeffdd6315cd)
Diffstat (limited to 'support')
-rw-r--r-- | support/java/src/android/support/v8/renderscript/RenderScript.java | 76 |
1 files changed, 50 insertions, 26 deletions
diff --git a/support/java/src/android/support/v8/renderscript/RenderScript.java b/support/java/src/android/support/v8/renderscript/RenderScript.java index 85b6763b..88accae2 100644 --- a/support/java/src/android/support/v8/renderscript/RenderScript.java +++ b/support/java/src/android/support/v8/renderscript/RenderScript.java @@ -1045,6 +1045,7 @@ public class RenderScript { } long mContext; + private boolean mDestroyed = false; //Dummy device & context for Inc Support Lib long mIncCon; //indicator of whether inc support lib has been loaded or not. @@ -1657,6 +1658,54 @@ public class RenderScript { nContextFinish(); } + private void helpDestroy() { + boolean shouldDestroy = false; + synchronized(this) { + if (!mDestroyed) { + shouldDestroy = true; + mDestroyed = true; + } + } + + if (shouldDestroy) { + nContextFinish(); + if (mIncCon != 0) { + nIncContextFinish(); + nIncContextDestroy(); + mIncCon = 0; + } + nContextDeinitToClient(mContext); + mMessageThread.mRun = false; + // Interrupt mMessageThread so it gets to see immediately that mRun is false + // and exit rightaway. + mMessageThread.interrupt(); + + // Wait for mMessageThread to join. Try in a loop, in case this thread gets interrupted + // during the wait. If interrupted, set the "interrupted" status of the current thread. + boolean hasJoined = false, interrupted = false; + while (!hasJoined) { + try { + mMessageThread.join(); + hasJoined = true; + } catch (InterruptedException e) { + interrupted = true; + } + } + if (interrupted) { + Log.v(LOG_TAG, "Interrupted during wait for MessageThread to join"); + Thread.currentThread().interrupt(); + } + + nContextDestroy(); + } + } + + @Override + 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 @@ -1672,32 +1721,7 @@ public class RenderScript { return; } validate(); - nContextFinish(); - if (mIncCon != 0) { - nIncContextFinish(); - nIncContextDestroy(); - mIncCon = 0; - } - nContextDeinitToClient(mContext); - mMessageThread.mRun = false; - - // Wait for mMessageThread to join. Try in a loop, in case this thread gets interrupted - // during the wait. If interrupted, set the "interrupted" status of the current thread. - boolean hasJoined = false, interrupted = false; - while (!hasJoined) { - try { - mMessageThread.join(); - hasJoined = true; - } catch (InterruptedException e) { - interrupted = true; - } - } - if (interrupted) { - Log.v(LOG_TAG, "Interrupted during wait for MessageThread to join"); - Thread.currentThread().interrupt(); - } - - nContextDestroy(); + helpDestroy(); } boolean isAlive() { |