{"id":261,"date":"2007-10-31T15:10:53","date_gmt":"2007-10-31T22:10:53","guid":{"rendered":"http:\/\/www.corbinstreehouse.com\/blog\/?p=261"},"modified":"2018-10-23T18:09:17","modified_gmt":"2018-10-24T01:09:17","slug":"instruments-on-leopard-how-to-debug-those-random-crashes-in-your-cocoa-app","status":"publish","type":"post","link":"https:\/\/www.corbinstreehouse.com\/blog\/2007\/10\/instruments-on-leopard-how-to-debug-those-random-crashes-in-your-cocoa-app\/","title":{"rendered":"Instruments on Leopard: How to debug those random crashes in your Cocoa app"},"content":{"rendered":"<p id=\"top\" \/>\n<p><b>Update: Sept 9, 2009:<\/b> Mac OS 10.6 Snow Leopard now has this feature built into Instruments! In Xcode, choose &#8220;Run -> Run with Performance Tool -> Zombies&#8221;, and repeat your steps that cause the crash. Easy as pie. Read on if you don&#8217;t have Snow Leopard&#8230;<\/p>\n<p><a href=\"http:\/\/www.apple.com\/macosx\/\">Mac OS 10.5 Leopard<\/a> has a great new developer app called <a href=\"http:\/\/\">Instruments<\/a>.<\/p>\n<p>It can easily be used to debug those &#8220;random crashers&#8221; in your application caused by too many -release or -autorelease calls. Let&#8217;s see how.<\/p>\n<p>If you want to follow along, download this project: <a href=\"\/blog\/wp-content\/uploads\/CrashTestDummy.zip\" title=\"CrashTestDummy.zip\">CrashTestDummy.zip<\/a><\/p>\n<p>Open it up in Xcode.<\/p>\n<p>The nib has two outlets in it: a basic NSObject called <span style=\"color: #3F6E74; font-family: Monaco; font-size: 10px;\">testObject<\/span> and button setup to invoke the following code:<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco\">&#8211; (<span style=\"color: #aa0d91\">IBAction<\/span>)buttonCrashTestAction:(<span style=\"color: #aa0d91\">id<\/span>)sender {<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #007400\">\/\/ We are going to autorelease the testObject too many times..<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #2e0d6e\"><span style=\"color: #000000\">[<\/span><span style=\"color: #3f6e74\">testObject<\/span> autorelease<span style=\"color: #000000\">];<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco\">}<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\">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 <strong>Object Allocations<\/strong> template:<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/ObjectAllocationsinInstruments.jpg\" width=\"654\" height=\"524\" \/><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\">Hit the purple &#8220;i&#8221; next to the instruments to bring up the inspector for it, and be sure to check &#8220;Record reference counts&#8221;, as seen below:<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/instruments-ichecked.jpg\" width=\"501\" height=\"255\" \/><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\">Cool. Click &#8220;Launch Executable&#8221; and select &#8220;Choose Executable&#8221;, and find CrashTestDummy.app from the built directory.<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/LaunchExecutable.jpg\" width=\"469\" height=\"308\" \/><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\">While you have the Open Panel up, add the environment variable NSZombieEnabled and set it to YES (sort of an optional step):<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/NSZombieEnabledSetToYes.jpg\" width=\"598\" height=\"532\" \/><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\">Hit Open, if you haven&#8217;t already done so.<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\">Hit &#8220;Record&#8221; in instruments, which will start the app, and click the &#8220;Crash Test&#8221; button a few times until the app crashes.<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\">Alright! Fire up Console.app and look for a line like this:<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/ConsoleOutput.jpg\" width=\"1099\" height=\"38\" \/><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\">The important part is the address, 0x150d40, in this case.<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\">In Instruments, move the mouse to the right of &#8220;All Allocations&#8221; and a little triangle appears:<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/LittleTriangleToTlick.jpg\" width=\"181\" height=\"55\" \/><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\">Click on it to see all the allocations.<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\">In the lower right hand corner of instruments click on the magnifying glass in the NSSearchField and select &#8220;Address&#8221;:<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/selectAddress.jpg\" width=\"245\" height=\"175\" \/><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\">Type in your address, and you should see all allocations at that address:<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/AddressFiltering.jpg\" width=\"781\" height=\"210\" \/><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\">Now, why so many? Well, addresses are reused. Chances are, the <strong>last<\/strong> allocation is your object, and sure enough, it is, since we know we are over releasing an <strong>NSObject<\/strong> instance. Click on that item, and you&#8217;ll see the history for it. Scroll to the bottom to see just the history for that NSObject:<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/HistoryForObject.jpg\" width=\"778\" height=\"56\" \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\">Now! You should be able to figure out which release probably shouldn&#8217;t be there. We know it is #19 from our demo, but sometimes it isn&#8217;t that easy to figure out. Select #19&#8217;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:<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/InstrumentsBottomButton.jpg\" width=\"34\" height=\"28\" \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\">You can then see when that &#8220;bad release&#8221; happened. Click on the thumb below for a full size screen shot of it.<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/>\n<a href=\"\/blog\/wp-content\/uploads\/TheBadReleaseInInstruments.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/TheBadReleaseInInstruments-tm.jpg\" width=\"300\" height=\"206\" \/><\/a><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\">Ahh! Way cool. Of course, this was a simple and easy test case.<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\">Happy debugging :)<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\"><br \/><\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"font-family: Helvetica; font-size: 12px;\">corbin<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update: Sept 9, 2009: Mac OS 10.6 Snow Leopard now has this feature built into Instruments! In Xcode, choose &#8220;Run -> Run with Performance Tool -> Zombies&#8221;, and repeat your steps that cause the crash&#8230;. <a class=\"read-more\" href=\"https:\/\/www.corbinstreehouse.com\/blog\/2007\/10\/instruments-on-leopard-how-to-debug-those-random-crashes-in-your-cocoa-app\/\">[read more]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4,6,86],"tags":[72],"class_list":["post-261","post","type-post","status-publish","format-standard","hentry","category-apple","category-cocoa","category-coding","tag-programming"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/comments?post=261"}],"version-history":[{"count":1,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/261\/revisions"}],"predecessor-version":[{"id":5181,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/261\/revisions\/5181"}],"wp:attachment":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/media?parent=261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/categories?post=261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/tags?post=261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}