使用相机时切入后台报错:Thread1:EXC_BAD_ACCESS(code=1,address=0x1)解决办法之一

博客讨论了在iOS应用中遇到的一个问题,当应用从活跃状态切到后台时,由于ARC导致Thread1: EXC_BAD_ACCESS(code=1,address=0x1)错误。作者指出,程序在后台时不执行disappear方法。通过参考第三方资源,建议采用监听应用状态变化的方式,手动调用disappear方法来解决这个问题。" 109696905,50230,AutoLisp应用管理平台:@lisp详解,"['lisp', '开发语言', 'AutoCAD插件']

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

问题是ARC错误release的对象再次被release。

程序切入后台时不会走disapear方法

参考了http://songzengbin.github.io后总结如下

加入监听监听切入后台或进入前台,手动执行disappear

在init中

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterBackgorund:) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];

    #pragma mark application  enterBackgorund enterForeground
-(void)enterBackgorund:(NSNotification *)notif
{
[self viewWillDisappear:YES];
}

-(void)enterForeground:(NSNotification *)notif
{
[self viewWillAppear:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
[self.mapView viewWillAppear];
self.isMapViewShowing = !self.mapView.hidden;
self.mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
}
- (void)viewWillDisappear:(BOOL)animated
{
[self.mapView viewWillDisappear];
self.isMapViewShowing = NO;
self.mapView.delegate = nil; // 不用时,置nil
}
在dealloc中
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值