Android/Android 일반

Nought(7.0)미만 WifiManager 메모리 누수 문제

lipnus 2019. 3. 21. 16:42
반응형

Android Nought(7.0)미만에서 WindowWifi사용시 메모리 누수문제



Android Document

WifiManager

public class WifiManager 
extends Object 

java.lang.Object
   ↳android.net.wifi.WifiManager



This class provides the primary API for managing all aspects of Wi-Fi connectivity.

On releases before Build.VERSION_CODES.N, this object should only be obtained from an Context#getApplicationContext(), and not from any other derived context to avoid memory leaks within the calling process.



StackOverflow

As the error suggests, it seems that WiFiManager must use the ApplicationContext, as opposed to the ActivityContext, otherwise a memory leak can occur. The error was triggered by following code:

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

To fix the issue I replaced the above line with:

WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);



수정전

wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager


수정후

wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager




Document: https://developer.android.com/reference/android/net/wifi/WifiManager

StackOverflow: https://stackoverflow.com/questions/42621010/signed-apk-error-wifimanagerleak

반응형

'Android > Android 일반' 카테고리의 다른 글

Service(unbind, bind)  (0) 2019.03.25
AIDL(Android Interface Definition Language)  (0) 2019.03.25
Intent Flag  (0) 2019.03.20
AVD(에뮬레이터) 삭제하는 방법  (0) 2019.03.20
In App Purchase  (0) 2019.03.13