From 3444dbe025ad78cae9fd77a4d2203a1a806ae1af Mon Sep 17 00:00:00 2001 From: Yang Ni Date: Thu, 7 Apr 2016 11:17:59 -0700 Subject: Destroy Type created by Allocation.createSized() Bug: 27972184 Change-Id: I4f80c74079087eab20b75a701c56cde141322462 --- rs/java/android/renderscript/Allocation.java | 32 ++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/rs/java/android/renderscript/Allocation.java b/rs/java/android/renderscript/Allocation.java index b303b4892565..7619e799ca9e 100644 --- a/rs/java/android/renderscript/Allocation.java +++ b/rs/java/android/renderscript/Allocation.java @@ -58,6 +58,7 @@ public class Allocation extends BaseObj { private static final int MAX_NUMBER_IO_INPUT_ALLOC = 16; Type mType; + boolean mOwningType = false; Bitmap mBitmap; int mUsage; Allocation mAdaptedAllocation; @@ -383,13 +384,16 @@ public class Allocation extends BaseObj { guard.open("destroy"); } - Allocation(long id, RenderScript rs, Type t, int usage, MipmapControl mips) { + Allocation(long id, RenderScript rs, Type t, boolean owningType, int usage, MipmapControl mips) { this(id, rs, t, usage); + mOwningType = owningType; mMipmapControl = mips; } protected void finalize() throws Throwable { RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize); + // Set mType null to avoid double-destroying it in case its finalizer races ahead + mType = null; super.finalize(); } @@ -1578,6 +1582,9 @@ public class Allocation extends BaseObj { mRS.finish(); // Necessary because resize is fifoed and update is async. long typeID = mRS.nAllocationGetType(getID(mRS)); + // Sets zero the mID so that the finalizer of the old mType value won't + // destroy the native object that is being reused. + mType.setID(0); mType = new Type(typeID, mRS); mType.updateFromNative(); updateCacheInfo(mType); @@ -1921,7 +1928,7 @@ public class Allocation extends BaseObj { if (id == 0) { throw new RSRuntimeException("Allocation creation failed."); } - return new Allocation(id, rs, type, usage, mips); + return new Allocation(id, rs, type, false, usage, mips); } finally { Trace.traceEnd(RenderScript.TRACE_TAG); } @@ -1979,7 +1986,7 @@ public class Allocation extends BaseObj { if (id == 0) { throw new RSRuntimeException("Allocation creation failed."); } - return new Allocation(id, rs, t, usage, MipmapControl.MIPMAP_NONE); + return new Allocation(id, rs, t, true, usage, MipmapControl.MIPMAP_NONE); } finally { Trace.traceEnd(RenderScript.TRACE_TAG); } @@ -2068,7 +2075,7 @@ public class Allocation extends BaseObj { } // keep a reference to the Bitmap around to prevent GC - Allocation alloc = new Allocation(id, rs, t, usage, mips); + Allocation alloc = new Allocation(id, rs, t, true, usage, mips); alloc.setBitmap(b); return alloc; } @@ -2078,7 +2085,7 @@ public class Allocation extends BaseObj { if (id == 0) { throw new RSRuntimeException("Load failed."); } - return new Allocation(id, rs, t, usage, mips); + return new Allocation(id, rs, t, true, usage, mips); } finally { Trace.traceEnd(RenderScript.TRACE_TAG); } @@ -2152,7 +2159,7 @@ public class Allocation extends BaseObj { } for (int i=1; i