当前位置:网站首页>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);
}
边栏推荐
猜你喜欢

The longest selling mobile phone in China has been selling well since its launch, crushing iphone12

5. TPM module initialization

友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)

Kongyiji's first question: how much do you know about service communication?
![[go] go implements row column conversion of sets](/img/d9/6272e55b2d9c6b6fbdf2537773bb83.png)
[go] go implements row column conversion of sets

Cmu15445 (fall 2019) project 1 - buffer pool details

解读创客教育所蕴含的科技素养

XJY-220/43AC220V静态信号继电器

Koa koa-combine-routers 分路由管理

ESP8266 RC522
随机推荐
2022 is half way through. It's hard to make money
Orb-slam2 source code learning (II) map initialization
[2023 MediaTek approved the test questions in advance] ~ questions and reference answers
C # generates PPK files in putty format (supports passphrase)
Oracle table creation and management
用recyclerReview展示Banner,很简单
A proper job is a good job
The communication mechanism and extension of Supervisor
【go】go 实现行专列 将集合进行转列
Exercises on recursion in C language
js中把数字转换成汉字输出
Packing and unpacking of C #
Is the public read-only field with immutable structure valid- Does using public readonly fields for immutable structs work?
【网络丢包,网络延迟?这款神器帮你搞定所有!】
Cmu15445 (fall 2019) project 1 - buffer pool details
pull_ to_ refresh
C # Generate PPK files in Putty format (passthrough support)
Experiment 8 T-SQL, stored procedure
Some views on libco
酒旅板块复苏,亚朵继续上市梦,距离“新住宿经济第一股“还有多远?