iPhone Memory Debugging with NSZombie and Instruments

When your iPhone app crashes with ‘BAD ACCESS’ you’re in trouble – a memory bug where you tried to call a method on a object that was already deleted. Instruments has support for NSZombie – a feature that makes it easy to find the source of the bug by showing you a full history of every alloc, retain, release, and autorelease of the object that caused the crash! Wow. Here’s how:

Basic steps are:

  1. Run in Performance tool ‘Object Allocations’
  2. Stop running and set options on ObjAllocations instrument: ‘Enable NSZombie’ Detection and ‘Record Reference Counts’
  3. Re-run from instruments, when it crashes, click the arrow in the popup zombie dialog
  4. Open up the stack trace view and see the full memory history of the problem object
  5. Wow, how awesome is that?
  6. If you love this post, do me a favor and check out our Free app Focus for Facebook
  7. You might also like my memory management tutorial and other posts under ‘App Development’.
Enable Zombies in Instruments

Enable Zombies in Instruments

Here’s a link to the demo program: ZombieDebug Demo Project . The code we’re looking at in the video is:

@implementation ZombieDebugViewController



@synthesize objArray;



-(void)rewriteText {

NSMutableString* s = [NSMutableString stringWithCapacity:100];

for (id obj in objArray) {

[s appendFormat:@"%@,/n",obj];

[obj release];

}

label.text = s;

}



- (void)viewDidLoad {

[super viewDidLoad];

self.objArray = [NSMutableArray arrayWithCapacity:10];

[objArray addObject:@"I'm a string object"];

[self rewriteText];

}



-(IBAction) tapButton:(id)button {

NSNumber* n = [NSNumber numberWithLong:random()];

[objArray addObject:n];

[self rewriteText];

}



-(void)dealloc {

[super dealloc];

self.objArray=nil;

}



@end

From: http://www.markj.net/iphone-memory-debug-nszombie/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值