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

Commit 9b643671 authored by Narayan Kamath's avatar Narayan Kamath
Browse files

SpellCheckerSession: Fix brittle finalizer pattern.

Always call super.finalize, also make sure super-class finalizers
are called last (i.e in reverse order of construction). While we're
here, we can switch to dalvik.system.CloseGuard to warn if the object
hasn't been properly closed.

Test: make
Bug: 35609098
Change-Id: I4e00c3a2cec93d1dacff20546e481fe757279661
parent 058a73e7
Loading
Loading
Loading
Loading
+14 −8
Original line number Diff line number Diff line
@@ -29,6 +29,8 @@ import com.android.internal.textservice.ISpellCheckerSessionListener;
import com.android.internal.textservice.ITextServicesManager;
import com.android.internal.textservice.ITextServicesSessionListener;

import dalvik.system.CloseGuard;

import java.util.LinkedList;
import java.util.Queue;

@@ -98,7 +100,7 @@ public class SpellCheckerSession {
    private final SpellCheckerSessionListener mSpellCheckerSessionListener;
    private final SpellCheckerSessionListenerImpl mSpellCheckerSessionListenerImpl;

    private boolean mIsUsed;
    private final CloseGuard mGuard = CloseGuard.get();

    /** Handler that will execute the main tasks */
    private final Handler mHandler = new Handler() {
@@ -128,8 +130,9 @@ public class SpellCheckerSession {
        mSpellCheckerSessionListenerImpl = new SpellCheckerSessionListenerImpl(mHandler);
        mInternalListener = new InternalListener(mSpellCheckerSessionListenerImpl);
        mTextServicesManager = tsm;
        mIsUsed = true;
        mSpellCheckerSessionListener = listener;

        mGuard.open("finishSession");
    }

    /**
@@ -160,7 +163,7 @@ public class SpellCheckerSession {
     * checker.
     */
    public void close() {
        mIsUsed = false;
        mGuard.close();
        try {
            mSpellCheckerSessionListenerImpl.close();
            mTextServicesManager.finishSpellCheckerService(mSpellCheckerSessionListenerImpl);
@@ -542,12 +545,15 @@ public class SpellCheckerSession {

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        if (mIsUsed) {
            Log.e(TAG, "SpellCheckerSession was not finished properly." +
                    "You should call finishSession() when you finished to use a spell checker.");
        try {
            // Note that mGuard will be null if the constructor threw.
            if (mGuard != null) {
                mGuard.warnIfOpen();
                close();
            }
        } finally {
            super.finalize();
        }
    }

    /**