Merge "[NAN] Update finalize to check and warn user if resources not freed" into mm-wireless-dev

This commit is contained in:
Etan Cohen
2016-08-19 17:22:26 +00:00
committed by Android Partner Code Review
2 changed files with 27 additions and 8 deletions

View File

@@ -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,8 +376,12 @@ public class WifiNanManager {
@Override
protected void finalize() throws Throwable {
disconnect();
super.finalize();
try {
mCloseGuard.warnIfOpen();
disconnect();
} finally {
super.finalize();
}
}
/**

View File

@@ -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,17 +99,19 @@ public class WifiNanSession {
}
mTerminated = true;
mMgr.clear();
mCloseGuard.close();
}
@Override
protected void finalize() throws Throwable {
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");
terminate();
try {
if (!mTerminated) {
mCloseGuard.warnIfOpen();
terminate();
}
} finally {
super.finalize();
}
super.finalize();
}
/**