关于 EXC_BAD_ACCESS

本文探讨了Objective-C中常见的内存管理问题——EXC_BAD_ACCESS错误的原因及解决办法。通过实例分析了autorelease对象的不当使用导致的问题,并给出了跟踪此类问题的方法。

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

简单看了下 Objective-c 的语法就开始Iphone开发了,

所以出各种奇怪问题都不奇怪。

根据官方解释 http://developer.apple.com/qa/qa2004/qa1367.html

出现 EXC_BAD_ACCESS 原因在于过度 release

而如果你程序中没有过度 release 就应该检查下 使用了类似 [NSString stringwithformat""]的函数

这种方法将会返回 autorelease 对象

 

我出现的情况就是使用了 [NSMutableArray arrayWithCapacity] 方法。该死。

 

But if you get something back from just about anything else including factory methods (e.g. [NSString stringWithFormat]) then you'll have an autorelease reference, which means it could be released at some time in the future by other code - so it is vital that if you need to keep it around beyond the immediate function that you retain it. If you don't, the memory may remain allocated while you are using it, or be released but coincidentally still valid, during your emulator testing, but is more likely to be released and show up as bad access errors when running on the device.

The best way to track these things down, and a good idea anyway (even if there are no apparent problems) is to run the app in the Instruments tool, especially with the Leaks option.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值