佳为好友

转:How to debug those random crashes in your Cocoa app -非常强悍的调试技巧

日期:2011.06.07

[自:非常强悍的调试技巧。
iphone也可以用,但是有些地方不同了。选项和名字。]

转:http://www.corbinstreehouse.com/blog/2007/10/instruments-on- leopard-how-to-debug-those-random-crashes-in-your-cocoa-app/

Update: Sept 9, 2009: Mac OS 10.6 Snow Leopard now has this feature built into Instruments! In Xcode, choose “Run -> Run with Performance Tool -> Zombies”, and repeat your steps that cause the crash. Easy as pie. Read on if you don’t have Snow Leopard…

Mac OS 10.5 Leopard has a great new developer app called Instruments.

It can easily be used to debug those “random crashers” in your application caused by too many -release or -autorelease calls. Let’s see how.

If you want to follow along, download this project: CrashTestDummy.zip

Open it up in Xcode.

The nib has two outlets in it: a basic NSObject called testObject and button setup to invoke the following code:

- (IBAction)buttonCrashTestAction:(id)sender {

// We are going to autorelease the testObject too many times..

[testObject autorelease];

}

So, we will hit the button a few times and we should get a crash. Compile the app with the Debug target. Start Instruments, and select the Object Allocations template:



Hit the purple “i” next to the instruments to bring up the inspector for it, and be sure to check “Record reference counts”, as seen below:





Cool. Click “Launch Executable” and select “Choose Executable”, and find CrashTestDummy.app from the built directory.





While you have the Open Panel up, add the environment variable NSZombieEnabled and set it to YES (sort of an optional step):




Hit Open, if you haven’t already done so.


Hit “Record” in instruments, which will start the app, and click the “Crash Test” button a few times until the app crashes.


Alright! Fire up Console.app and look for a line like this:



The important part is the address, 0x150d40, in this case.


In Instruments, move the mouse to the right of “All Allocations” and a little triangle appears:




Click on it to see all the allocations.

In the lower right hand corner of instruments click on the magnifying glass in the NSSearchField and select “Address”:



Type in your address, and you should see all allocations at that address:



Now, why so many? Well, addresses are reused. Chances are, the last allocation is your object, and sure enough, it is, since we know we are over releasing an NSObject instance. Click on that item, and you’ll see the history for it. Scroll to the bottom to see just the history for that NSObject:



Now! You should be able to figure out which release probably shouldn’t be there. We know it is #19 from our demo, but sometimes it isn’t that easy to figure out. Select #19′s row and then click the little gray arrow to next to its address (or whatever yours is), and then click on this button at the bottom of instruments to show the stack trace / extended detail:



You can then see when that “bad release” happened. Click on the thumb below for a full size screen shot of it.




Ahh! Way cool. Of course, this was a simple and easy test case.


Happy debugging :)


corbin



+++++

posted on 2012-12-25 10:04 佳为好友 阅读(303) 评论(0)  编辑 收藏 引用 所属分类: Debug-GDB


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2012年12月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

留言簿(1)

随笔分类

搜索

最新评论

评论排行榜