137_Handler警告解决方法

本文解析了Android中使用Handler时可能引发内存泄漏的原因,并提供了一种通过弱引用保持外部类引用的方法来避免这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Handler警告解决方法



Handler的时候

经常出现一个警告

This Handler class should be static or leaks might occur

这个Handler类应该被设成static,否则有可能内存泄漏

 


为什么呢

 

原因:

首先我们复习一下handler机制

消息发送到主线程,添加到消息队列中,然后Looper循环接收消息

 

注意了,在消息队列里面排队的消息保持了对目标Handler类的引用,

如果Handler是个内部类,那么它也会保持它所在的外部类(一般就是ActivityService),

只要有消息在队列里面,

那么Handler就无法被回收,

如果Handler不是static,那么用HandlerActivityService也不能被回收

这就导致了内存泄漏

 

 

解决:

 

我们可以在Handler中保持一个外部类的弱引用

 

来个static,继承Handler

public static class MyHandler extends Handler{

        WeakReference mOuterClass;

        MyHandler(OuterClass clz) {

                mOuterClass clz= new WeakReference(clz);

        }

 

     //成员变量弱引用=外部类引用

 

        @Override

        public void handleMessage(Message msg) {

            OuterClass clz= mOuterClass.get();

            switch (msg.what) {

            case what:

                clz.method();

                break;

            defaule:

                break;

            }

        }

 

}

 

 

这样的话传入的外部类就都是弱引用了

 

 

声明之后

就可以用handler

private MyHandler handler = new MyHandler(this);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

C:\Users\Administrator\Documents\Arduino\sketch_jun18a_copy_20250620142542_copy_20250706090617\sketch_jun18a_copy_20250620142542_copy_20250706090617.ino:61:18: error: variable or field 'custom_exception_handler' declared void 61 | extern "C" void custom_exception_handler(EXCEPTION_INFO* exc_info) { | ^~~~~~~~~~~~~~~~~~~~~~~~ C:\Users\Administrator\Documents\Arduino\sketch_jun18a_copy_20250620142542_copy_20250706090617\sketch_jun18a_copy_20250620142542_copy_20250706090617.ino:61:43: error: 'EXCEPTION_INFO' was not declared in this scope 61 | extern "C" void custom_exception_handler(EXCEPTION_INFO* exc_info) { | ^~~~~~~~~~~~~~ C:\Users\Administrator\Documents\Arduino\sketch_jun18a_copy_20250620142542_copy_20250706090617\sketch_jun18a_copy_20250620142542_copy_20250706090617.ino:61:59: error: 'exc_info' was not declared in this scope 61 | extern "C" void custom_exception_handler(EXCEPTION_INFO* exc_info) { | ^~~~~~~~ C:\Users\Administrator\Documents\Arduino\sketch_jun18a_copy_20250620142542_copy_20250706090617\sketch_jun18a_copy_20250620142542_copy_20250706090617.ino:61:17: error: variable or field 'custom_exception_handler' declared void 61 | extern "C" void custom_exception_handler(EXCEPTION_INFO* exc_info) { | ^~~~~~~~~~~~~~~~~~~~~~~~ C:\Users\Administrator\Documents\Arduino\sketch_jun18a_copy_20250620142542_copy_20250706090617\sketch_jun18a_copy_20250620142542_copy_20250706090617.ino:61:42: error: 'EXCEPTION_INFO' was not declared in this scope 61 | extern "C" void custom_exception_handler(EXCEPTION_INFO* exc_info) { | ^~~~~~~~~~~~~~ C:\Users\Administrator\Documents\Arduino\sketch_jun18a_copy_20250620142542_copy_20250706090617\sketch_jun18a_copy_20250620142542_copy_20250706090617.ino:61:58: error: 'exc_info' was not declared in this scope 61 | extern "C" void custom_exception_handler(EXCEPTION_INFO* exc_info) { | ^~~~~~~~ C:\Users\Administrator\Documents\Arduino\sketch_jun18a_copy_20250620142542_copy_20250706090617\sketch_jun18a_copy_20250620142542_copy_20250706090617.ino: In function 'void setup()': C:\Users\Administrator\Documents\Arduino\sketch_jun18a_copy_20250620142542_copy_20250706090617\sketch_jun18a_copy_20250620142542_copy_20250706090617.ino:143:27: error: 'custom_exception_handler' was not declared in this scope; did you mean '_xtos_set_exception_handler'? 143 | set_exception_handler(custom_exception_handler); | ^~~~~~~~~~~~~~~~~~~~~~~~ | _xtos_set_exception_handler C:\Users\Administrator\Documents\Arduino\sketch_jun18a_copy_20250620142542_copy_20250706090617\sketch_jun18a_copy_20250620142542_copy_20250706090617.ino:143:5: error: 'set_exception_handler' was not declared in this scope; did you mean '_xtos_set_exception_handler'? 143 | set_exception_handler(custom_exception_handler); | ^~~~~~~~~~~~~~~~~~~~~ | _xtos_set_exception_handler exit status 1 Compilation error: variable or field 'custom_exception_handler' declared void
最新发布
07-25
../Core/Src/main.c:34:23: error: '_estack' undeclared here (not in a function) 34 | (void (*)(void))(&_estack), // 初始栈指针 | ^~~~~~~ ../Core/Src/main.c:35:5: error: 'Reset_Handler' undeclared here (not in a function) 35 | Reset_Handler, // 复位处理程序 | ^~~~~~~~~~~~~ ../Core/Src/main.c:36:5: error: 'NMI_Handler' undeclared here (not in a function); did you mean 'Error_Handler'? 36 | NMI_Handler, // NMI 处理程序 | ^~~~~~~~~~~ | Error_Handler ../Core/Src/main.c:37:5: error: 'HardFault_Handler' undeclared here (not in a function) 37 | HardFault_Handler, // 硬件错误处理程序 | ^~~~~~~~~~~~~~~~~ ../Core/Src/main.c:39:5: error: 'SVC_Handler' undeclared here (not in a function); did you mean 'Error_Handler'? 39 | SVC_Handler, // SVC 处理程序 | ^~~~~~~~~~~ | Error_Handler ../Core/Src/main.c:41:5: error: 'PendSV_Handler' undeclared here (not in a function) 41 | PendSV_Handler, // PendSV 处理程序 | ^~~~~~~~~~~~~~ ../Core/Src/main.c:42:5: error: 'SysTick_Handler' undeclared here (not in a function) 42 | SysTick_Handler, // SysTick 处理程序(关键) | ^~~~~~~~~~~~~~~ ../Core/Src/main.c:49:1: warning: 'always_inline' attribute ignored due to conflict with 'noinline' attribute [-Wattributes] 49 | { | ^ ../Core/Src/main.c: In function 'Configure_Interrupt_Priorities': ../Core/Src/main.c:61:5: warning: implicit declaration of function 'HAL_NVIC_SetPriorityGrouping'; did you mean 'NVIC_SetPriorityGrouping'? [-Wimplicit-function-declaration] 61 | HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ | NVIC_SetPriorityGrouping ../Core/Src/main.c:61:34: error: 'NVIC_PRIORITYGROUP_0' undeclared (first use in this function) 61 | HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0); | ^~~~~~~~~~~~~~~~~~~~ ../Core/Src/main.c:61:34: note: each undeclared identifier is reported only once for each function it appears in make: *** [Core/Src/subdir.mk:49: Core/Src/main.o] Error 1 make: *** Waiting for unfinished jobs.... "make -j12 all" terminated with exit code 2. Build might be incomplete.
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值