Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit eb4dd08e authored by Yang Ni's avatar Yang Ni
Browse files

Added CloseGuard for BaseObj

Bug: 27719830

To turn on warnings, apps have to add to their Activity.onCreate() method
the following code.

        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                               .detectLeakedClosableObjects()
                               .penaltyLog()
                               .build());

For Slang generated ScriptC derived classes, we assume their
constructors won't throw exceptions after calling the ScriptC
constructor. In addition, ScriptIntrinsic derived classes do not seem
to throw exceptions in their constructors either. Therefore, we can
leave the guard.open() call in the Script constructor. This may be
only an approximation, but allows us to add CloseGuard for script
objects without making changes to slang.

Change-Id: I77ed45239a60b85af5c811dee6c124fb53da9060
parent 72dd79fa
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -372,6 +372,7 @@ public class Allocation extends BaseObj {
            Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
            throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
        }
        guard.open("destroy");
    }

    Allocation(long id, RenderScript rs, Type t, int usage, MipmapControl mips) {
@@ -1907,6 +1908,7 @@ public class Allocation extends BaseObj {
            if (type.getID(rs) == 0) {
                throw new RSInvalidStateException("Bad Type");
            }
            // TODO: What if there is an exception after this? The native allocation would leak.
            long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
            if (id == 0) {
                throw new RSRuntimeException("Allocation creation failed.");
+11 −2
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.renderscript;

import dalvik.system.CloseGuard;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
@@ -69,6 +70,7 @@ public class BaseObj {
    }

    private long mID;
    final CloseGuard guard = CloseGuard.get();
    private boolean mDestroyed;
    private String mName;
    RenderScript mRS;
@@ -119,6 +121,7 @@ public class BaseObj {
        }

        if (shouldDestroy) {
            guard.close();
            // must include nObjDestroy in the critical section
            ReentrantReadWriteLock.ReadLock rlock = mRS.mRWLock.readLock();
            rlock.lock();
@@ -133,9 +136,15 @@ public class BaseObj {
    }

    protected void finalize() throws Throwable {
        try {
            if (guard != null) {
                guard.warnIfOpen();
            }
            helpDestroy();
        } finally {
            super.finalize();
        }
    }

    /**
     * Frees any native resources associated with this object.  The
+2 −0
Original line number Diff line number Diff line
@@ -808,6 +808,7 @@ public class Element extends BaseObj {
            mSize += mElements[ct].mSize * mArraySizes[ct];
        }
        updateVisibleSubElements();
        guard.open("destroy");
    }

    Element(long id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
@@ -827,6 +828,7 @@ public class Element extends BaseObj {
        mKind = dk;
        mNormalized = norm;
        mVectorSize = size;
        guard.open("destroy");
    }

    Element(long id, RenderScript rs) {
+1 −0
Original line number Diff line number Diff line
@@ -170,6 +170,7 @@ public class FileA3D extends BaseObj {
    FileA3D(long id, RenderScript rs, InputStream stream) {
        super(id, rs);
        mInputStream = stream;
        guard.open("destroy");
    }

    private void initEntries() {
+1 −0
Original line number Diff line number Diff line
@@ -150,6 +150,7 @@ public class Font extends BaseObj {

    Font(long id, RenderScript rs) {
        super(id, rs);
        guard.open("destroy");
    }

    /**
Loading