Corbin's Treehouse - Corbin Dunn, Santa Cruz, CA
Plug Bug
Treehouse
Photography
Videos
Projects
Unicycling
About

Archive for the 'Apple' Category

Instruments on Leopard: How to debug those random crashes in your Cocoa app

Wednesday, October 31st, 2007

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

New Leopard user features – Open and Save Panels

Tuesday, October 30th, 2007

Leopard, the new Mac OS 10.5, is out!

There are a lot of new features, but not everything is mentioned on the features page. Here are some of the cool “power user” features which you may not know about:

The Open and Save Panel (implemented in Cocoa, also known as the NSSavePanel and NSOpenPanel)

1. There is a new Icon View mode with options:

However, the way to change the icon size to be small (as seen above) isn’t obvious. Click and hold down on the icon view segmented cell and a popup will occur:

2. You can now insert items in the panel (and Finder) “side bar” by drag and dropping directly from the views (you couldn’t do this on Tiger!) You can also rearrange or remove items.

3. When you have an item selected, cmd-r will “reveal” it in Finder

4. When you have an item selected, cmd-i will give the Finder “info” window.

5. If you perform a search, you can save the search (just like in Finder). In addition, you can save it just for that application:

…and it appears in a special sidebar section for just that app (to remove it, drag it out):

That’s it for now. 5 new features not even included in the top 300!



Happy iPhone day!

Friday, June 29th, 2007

Today is a very exciting day. The iPhone is going to be available at 6pm. Let me tell you, it is wonderful! Go get one. You will not be disappointed.

NSBrowser, bindings, and a custom cell class

Thursday, June 21st, 2007

Hi Cocoa Programmers,

There is a small bug in NSBrowser if you use bindings and want a custom cell class. Typically, you would do something like this in your awakeFromNib:

- (void)awakeFromNib {
    [iBrowser setCellClass:[ImageTextBrowserCell class]];
}

However, if you are using bindings, it won’t work. The cells that were archived out in the nib file will not have the proper class. So, you have to nudge NSBrowser a bit to get it going:

- (void)awakeFromNib {
    [iBrowser setCellClass:[ImageTextBrowserCell class]];
    [iBrowser loadColumnZero];
}

Just a quick tip in case you run into this!

WWDC 2007

Thursday, June 14th, 2007

If you haven’t seen me at Apple WWDC 20007, I’m easy to “spot”:

Photo 11-1

In fact, I’m on the attendee page!

A greener apple

Wednesday, May 2nd, 2007

I’m an advocate for greener companies, and I’m happy to announce that the company I work for is striving for greener technology:

http://www.apple.com/hotnews/agreenerapple/

Spread the word.

Mac OS X tip: Apps in your Finder Toolbar!

Thursday, February 15th, 2007

Here is a very cool tip that I discovered from one of hour HI guys. You can put apps in your toolbar! For instance, I frequently drag files to TextEdit to open them. Having them in the toolbar is handy in case they aren’t in the Dock or I don’t want to drag too far:

Apps In Finder

To do this, right click on your toolbar (or ctrl-click if you don’t have a Mighty Mouse) and select “Customize Toolbar…”. Now, here is the trick; open up another Finder window and browse to the apps that you want to add to the toolbar. Simply drag them there, and they will stick! Very cool.

Xcode debugging tips

Wednesday, August 9th, 2006

It might be nice to note a few Xcode debugging things that I tend to do. Frequently, I’ll want to break on a specific symbol, like

-[NSException raise]

Well, there are several ways to do this.
One way is to drop to GDB and type “b -[NSException raise]”.
Another way is to bring up your breakpoints window in Xcode and add a symbolic breakpoint:

Double Click To Add Symbol

and type in the same thing, “-[NSException raise]”.

But wait! You can be lazy. Just type “raise” and Xcode will then prompt you to select the right symbol:

Select The Right Raise

You can also do this for a generic symbol, like “drawRect:” and then select just the one you want:

Select Draw Rect

Simple, and easy! Let Xcode do a lot of the work for you.
Some things to note:
1. Don’t bother typing the [ and ], or the minus or plus
2. Be sure to type the symbol name with the correct case and :’s.

People! Use your keyboard (shortcuts) for menus.

Friday, July 14th, 2006

Mac OS X has some GREAT keyboard access. One spectacular thing is the ability to access all items (unlike windows, where you have to have a defined shortcut for each item, and you easily run out of items).

First, go to the System Preferences.app and change the keyboard shortcut from Ctrl-F2 to Cmd-F2.

Picture 2

Cmd-F2 is so much easier to hit than Ctrl-F2.

Next, use it! Being from Windows, when I first used it I didn’t realize how it works. Basically, it is doing type-selection; ie: you type the word you want it to go to.

So, people are amazed at how fast I can reopen files with my fingers. For instance, to open the last file in TextEdit, I do this:

Cmd-F2 (focuses Apple menu)
F (Focuses “File”)
Enter (Drops down the “File” menu)
O (Selects “Open Recent” menu)
Enter (Selects the first item in the list)
Enter (Opens the item)

Sweeetness.

I use this all the time to open recent items in Xcode.

One small bug: the items in the Open Recent menu don’t work with type selection..hopefully that will get fixed.

Xcode tips and tricks — fast method finding

Friday, July 14th, 2006

Inside of Xcode, I’ll want to quickly go to a method implementation. If you hit Cmd-F and do a find for the method name (or the start of it), you will frequently get hits for calls of the method instead of just the implementation.

There is a quick way to find the implementation: Hit Cmd-F and type: “)methodName”.

The left paren will match only the implementation (provided you don’t put a space after it…which you shouldn’t do anyways).

I picked up this little tidbit by watching Troy Stephens code around. Thanks Troy!


(c) 2008-2012 Corbin Dunn

Corbin's Treehouse is powered by WordPress. Made on a Mac.

Subscribe to RSS feeds for entries and comments.

18 queries. 1.122 seconds.