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

Commit fd11ec15 authored by Etan Cohen's avatar Etan Cohen Committed by Mitchell Wills
Browse files

[NAN] Update finalize to check and warn user if resources not freed

Use CloseGuard to check that disconnect() is called to free the resources
allocated by connect(). finalize() will still call disconnect() but now
also print out errors (if StrictMode enabled for feature).

Bug: 28068773
Change-Id: I5f5847c0261ef9f79a21fdb935509a85761591cc
(cherry picked from commit 9d556496)
parent fc3c0a05
Loading
Loading
Loading
Loading
+12 −2
Original line number Diff line number Diff line
@@ -39,6 +39,8 @@ import android.util.SparseArray;

import com.android.internal.annotations.GuardedBy;

import dalvik.system.CloseGuard;

import libcore.util.HexEncoding;

import org.json.JSONException;
@@ -223,6 +225,7 @@ public class WifiNanManager {

    private final Context mContext;
    private final IWifiNanManager mService;
    private final CloseGuard mCloseGuard = CloseGuard.get();

    private final Object mLock = new Object(); // lock access to the following vars

@@ -333,6 +336,8 @@ public class WifiNanManager {
                throw e.rethrowFromSystemServer();
            }
        }

        mCloseGuard.open("disconnect");
    }

    /**
@@ -362,6 +367,7 @@ public class WifiNanManager {
            mClientId = INVALID_CLIENT_ID;
        }

        mCloseGuard.close();
        try {
            mService.disconnect(clientId, binder);
        } catch (RemoteException e) {
@@ -371,9 +377,13 @@ public class WifiNanManager {

    @Override
    protected void finalize() throws Throwable {
        try {
            mCloseGuard.warnIfOpen();
            disconnect();
        } finally {
            super.finalize();
        }
    }

    /**
     * Request a NAN publish session. The actual publish session is provided by
+15 −6
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ import android.annotation.SystemApi;
import android.net.wifi.RttManager;
import android.util.Log;

import dalvik.system.CloseGuard;

import java.lang.ref.WeakReference;

/**
@@ -52,6 +54,8 @@ public class WifiNanSession {
     */
    protected boolean mTerminated = false;

    private final CloseGuard mCloseGuard = CloseGuard.get();

    /**
     * {@hide}
     */
@@ -60,6 +64,8 @@ public class WifiNanSession {

        mMgr = new WeakReference<>(manager);
        mSessionId = sessionId;

        mCloseGuard.open("terminate");
    }

    /**
@@ -77,6 +83,7 @@ public class WifiNanSession {
        mgr.terminateSession(mSessionId);
        mTerminated = true;
        mMgr.clear();
        mCloseGuard.close();
    }

    /**
@@ -92,18 +99,20 @@ public class WifiNanSession {
        }
        mTerminated = true;
        mMgr.clear();
        mCloseGuard.close();
    }

    @Override
    protected void finalize() throws Throwable {
        try {
            if (!mTerminated) {
            Log.w(TAG, "WifiNanSession mSessionId=" + mSessionId
                    + " was not explicitly terminated. The session may use resources until "
                    + "terminated so step should be done explicitly");
                mCloseGuard.warnIfOpen();
                terminate();
            }
        } finally {
            super.finalize();
        }
    }

    /**
     * Sends a message to the specified destination. Message transmission is part of the current