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

Commit d22a6f09 authored by Jason Sams's avatar Jason Sams
Browse files

Implement finalizer for RS contexts.

Fixes memory leak when apps forget to call .destroy() on the context.

Change-Id: Ida4685768e92cfe3875da38846d17b86cc386cd0
parent e460f88d
Loading
Loading
Loading
Loading
+35 −13
Original line number Diff line number Diff line
@@ -938,6 +938,8 @@ public class RenderScript {

    long     mDev;
    long     mContext;
    private boolean mDestroyed = false;

    @SuppressWarnings({"FieldCanBeLocal"})
    MessageThread mMessageThread;

@@ -1382,14 +1384,16 @@ public class RenderScript {
        nContextFinish();
    }

    /**
     * Destroys this RenderScript context.  Once this function is called,
     * using this context or any objects belonging to this context is
     * illegal.
     *
     */
    public void destroy() {
        validate();
    private void helpDestroy() {
        boolean shouldDestroy = false;
        synchronized(this) {
            if (!mDestroyed) {
                shouldDestroy = true;
                mDestroyed = true;
            }
        }

        if (shouldDestroy) {
            nContextFinish();

            nContextDeinitToClient(mContext);
@@ -1404,6 +1408,24 @@ public class RenderScript {
            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
     * illegal.
     *
     */
    public void destroy() {
        validate();
        helpDestroy();
    }

    boolean isAlive() {
        return mContext != 0;