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

Commit 9d556496 authored by Etan Cohen's avatar Etan Cohen
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
parent 009b1fcb
Loading
Loading
Loading
Loading
+12 −2
Original line number Diff line number Diff line
@@ -38,6 +38,8 @@ import android.util.SparseArray;

import com.android.internal.annotations.GuardedBy;

import dalvik.system.CloseGuard;

import libcore.util.HexEncoding;

import org.json.JSONException;
@@ -221,6 +223,7 @@ public class WifiNanManager {
    public static final int WIFI_NAN_DATA_PATH_ROLE_RESPONDER = 1;

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

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

@@ -332,6 +335,8 @@ public class WifiNanManager {
                e.rethrowAsRuntimeException();
            }
        }

        mCloseGuard.open("disconnect");
    }

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

        mCloseGuard.close();
        try {
            mService.disconnect(clientId, binder);
        } catch (RemoteException e) {
@@ -370,9 +376,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