当前位置:网站首页>StrictMode分析Registion-StrictMode原理(4)
StrictMode分析Registion-StrictMode原理(4)
2022-07-01 00:40:00 【怪叔叔萝莉控】
4. Registion的泄漏问题
这里的Registion主要有两种,分别是Service、BroadCastReceiver:
4.2 检测时机
这里先补充一个前置知识点,stopService的流程:
Context.stopSerivce -> ActivityThread.STOP_SERVICE -> ActivityThread.scheduleContextCleanup() -> LoadApk.removeContextRegistrations
我们需要的检测时机,在removeContextRegistrations,这里面用到了两个成员变量:
//根据context,来存储当前的广播以及广播分发器
private final ArrayMap<Context, ArrayMap<BroadcastReceiver, ReceiverDispatcher>> mReceivers
= new ArrayMap<>();
//根据context,来存储service以及service的分发器
private final ArrayMap<Context, ArrayMap<ServiceConnection, LoadedApk.ServiceDispatcher>> mServices
= new ArrayMap<>();
public void removeContextRegistrations(Context context,
String who, String what) {
//是否开启了注册泄漏
final boolean reportRegistrationLeaks = StrictMode.vmRegistrationLeaksEnabled();
synchronized (mReceivers) {
//拿到与当前context相同的map
ArrayMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher> rmap =
mReceivers.remove(context);
if (rmap != null) {
//分析receiver的泄漏
for (int i = 0; i < rmap.size(); i++) {
LoadedApk.ReceiverDispatcher rd = rmap.valueAt(i);
IntentReceiverLeaked leak = new IntentReceiverLeaked(
what + " " + who + " has leaked IntentReceiver "
+ rd.getIntentReceiver() + " that was " +
"originally registered here. Are you missing a " +
"call to unregisterReceiver()?");
leak.setStackTrace(rd.getLocation().getStackTrace());
Slog.e(ActivityThread.TAG, leak.getMessage(), leak);
if (reportRegistrationLeaks) {
StrictMode.onIntentReceiverLeaked(leak);
}
try {
ActivityManager.getService().unregisterReceiver(
rd.getIIntentReceiver());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
}
mUnregisteredReceivers.remove(context);
}
synchronized (mServices) {
//Slog.i(TAG, "Receiver registrations: " + mReceivers);
//移除当前stop的service,并返回以当前service为key的map
ArrayMap<ServiceConnection, LoadedApk.ServiceDispatcher> smap =
mServices.remove(context);
if (smap != null) {
//分析service的泄漏
for (int i = 0; i < smap.size(); i++) {
LoadedApk.ServiceDispatcher sd = smap.valueAt(i);
ServiceConnectionLeaked leak = new ServiceConnectionLeaked(
what + " " + who + " has leaked ServiceConnection "
+ sd.getServiceConnection() + " that was originally bound here");
leak.setStackTrace(sd.getLocation().getStackTrace());
Slog.e(ActivityThread.TAG, leak.getMessage(), leak);
if (reportRegistrationLeaks) {
StrictMode.onServiceConnectionLeaked(leak);
}
try {
ActivityManager.getService().unbindService(
sd.getIServiceConnection());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
sd.doForget();
}
}
mUnboundServices.remove(context);
//Slog.i(TAG, "Service registrations: " + mServices);
}
边栏推荐
- A letter to 5000 fans!
- The question of IBL precomputation is finally solved
- 用recyclerReview展示Banner,很简单
- 【原创】 PLSQL 索引排序优化
- 冲击继电器ZC-23/DC220V
- pull_ to_ refresh
- Listview in flutter application development
- Flutter Error: Cannot run with sound null safety, because the following dependencies don‘t support
- 【网络丢包,网络延迟?这款神器帮你搞定所有!】
- HDU 2488 A Knight's Journey(DFS)
猜你喜欢

DX-11Q信号继电器

Gavin's insight on the transformer live broadcast course - rasa project's actual banking financial BOT Intelligent Business Dialogue robot system startup, language understanding, dialogue decision-mak

用recyclerReview展示Banner,很简单

NE555 waveform generator handle tutorial NE555 internal structure (I)

Koa koa-combine-routers 分路由管理

P4 learning - p4runtime

Analyzing the wisdom principle in maker education practice

DLS-42/6-4 DC110V双位置继电器

K210工地安全帽

Exploring the road of steam education innovation in the Internet Era
随机推荐
ESP8266 RC522
Left join displays the specified value when the left join matching data is null
For the first time in more than 20 years! CVPR best student thesis awarded to Chinese college students!
机器人编程的培训学科类原理
染色法判断二分图
软硬件基础知识学习--小日记(1)
K210门禁毕设
How to scroll uitableview to a specific position - how to scroll uitableview to specific position
Win11安装redis 数据库以及redis desktop manager的下载
[learning notes] structure
Windows环境下安装MongoDB数据库
Exploring the road of steam education innovation in the Internet Era
C # Generate PPK files in Putty format (passthrough support)
HDU 2488 A Knight's Journey(DFS)
The girlfriend said: if you want to understand the three MySQL logs, I will let you heiheihei!
Parity linked list [two general directions of linked list operation]
PHP online confusion encryption tutorial sharing + basically no solution
K210 access control complete
NE555 waveform generator handle tutorial NE555 internal structure (I)
Inspire students' diversified thinking with steam Education