当前位置:网站首页>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);
}
边栏推荐
- Dls-20 double position relay 220VDC
- 奇偶链表[链表操作的两种大方向]
- pytorch编程知识(2)
- Analyze the maker education path integrating the essence of discipline
- ORB-SLAM2源码学习(二)地图初始化
- Get screen height
- Set different background colors for the border and text of the button
- Usage of C set
- The quantity and quality of the devil's cold rice 101; Employee management; College entrance examination voluntary filling; Game architecture design
- Join table query select generation
猜你喜欢
随机推荐
fluttertoast
Green, green the reed. dew and frost gleam.
Technical personnel advanced to draw a big picture of business, hand-in-hand teaching is coming
[learning notes] simple DP
软硬件基础知识学习--小日记(1)
Sword finger offer 19 Regular Expression Matching
Two position relay st2-2l/ac220v
Two-stage RO: part 1
K210 site helmet
孔乙己第一问之服务通信知多少?
技术人进阶画业务大图,手把手教学来了
ArrayList analysis 1-cycle, capacity expansion, version
Is the public read-only field with immutable structure valid- Does using public readonly fields for immutable structs work?
文件服务设计
06.论Redis持久化的几种方式
Web compatibility testing of software testing
Tcp/ip protocol stack, about TCP_ RST | TCP_ ACK correct attitude
A proper job is a good job
Win11安装redis 数据库以及redis desktop manager的下载
机器人编程的培训学科类原理









