NSZombies and the Program received signal: “EXC_BAD_ACCESS” exception

[The following article applies to Xcode 3.x]

Even when following the memory management techniques explained by Steve "Scotty" Scott I sometimes run across my most dreaded runtime error;

Program received signal: “EXC_BAD_ACCESS”

This happens when you try to access an object that has already been released. Sadly the error message doesn't give you any information on what object you were trying to access, but luckily you can call up some zombies to help out.

Control-click the Executable you are trying to debug and select Get Info

Use the + button to add a new environment variable called NSZombieEnabled and set its value to YES.

When you run your app with this variable enabled, all deallocated objects are kept in memory and you will get a better error message in the console once one of them receives the unexpected call which crashes your app.

The debugger stack trace can at this point help you figure out exactly where in you code the problem occurred. Hovering the mouse cursor over the offending object will indicate that it is indeed an NSZombie.

Now that you know which object was accessed after being released you should be able to squash the bug in a few minutes by making sure to apply the memory management techniques correctly on it.

Make sure to uncheck the NSZombieEnabled variable before you ship your code, otherwise no memory is ever freed from you app. If you forget to uncheck it all released objects will be kept around in a zombie state, consuming your memory and eventually getting your app force killed by the system.

Since it is important and you need to remember this, say the following phrase out loud;

"I will not ship zombie infested code to my customers."

Launching the App Store from within your iPhone application

Today I decided to figure out how to launch the App Store application from within an iPhone application using the SDK.

It turns out this is pretty simple, but you have to beware of a few issues.
  • First and foremost, it doesn't work from the iPhone Simulator, in all probability since the App Store application is not available on the simulator.
  • Second you need to provide a valid URL to an application in the App Store. You can access the URL to an application by simply right clicking the Application in iTunes and selecting the "Copy iTunes Store URL" menu item.

Here is the core code needed.

[[UIApplication sharedApplication]
 openURL:[NSURL
  URLWithString:@"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291170459&mt=8"]];

The above URL points to my Hiragana application.

I then chose to store the App Store URL in the info.plist file under a key named TEBAppStoreLink, so that I can use the same code for any future applications.

I ended up with this method for launching the App Store with the URL in the info.plist file.

- (void)goToAppStore
{
  UIApplication *app = [UIApplication sharedApplication];

  // Get the url from info.plist.
  NSString *appStoreLink = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"TEBAppStoreLink"];

  // Open the url if it was available.
  if(appStoreLink){
  [app openURL:[NSURL URLWithString:appStoreLink]];
  }
}


What really happens is that Mobile Safari is launched with the provided URL, and upon realizing that the link is an App Store link it in turn launches the App Store application.

Of course, if you provide any other URL it just opens in Safari as usual. I haven't tested what happens if a YouTube URL is used, but I imagine the YouTube app would launch. That, however,  is not the problem i set out to solve today.

En route to Atlanta for the Cocoa Bootcamp

I'm just now at the airport, waiting to board the plane which will take me to Atlanta and the Cocoa Bootcamp. This is a trip I've been looking forward to for over a year.

I have prepared for the course by reading most of Aaron Hilegass' book Cocoa Programming for Mac OS X, and of course by meddling with iPhone development. But I have always found it easier to learn by interacting with knowledgeable people, so this will be my big chance to really get to grips with the parts of the Cocoa framework which I haven't been able to grasp. 

The Interface Builder has been my biggest stumbling block so far, leading to me writing all interfaces in my iPhone applications in code. So I hope to come away from this week with new insights into how it's supposed to be used. Interface Builder is a very different beast compared to the .NET Visual Studio experience I'm used to. However, I fully expect it to be superior once I can wrap my mind around it. It is an Apple product after all.

Judging by his book, Aaron has a knack for systematically introducing progressively more complex concepts in a very clear and easy to follow manner. I thoroughly recommend his book to anyone new to Objective-C and Cocoa. I think I may be able to finish it during the flight.

I'll summarize my experience at the Big Nerd Ranch in an upcoming post.

An introduction

Okay, so here's the deal.

I am a full time Windows application developer for a multinational software company. I love my job, I really truly do.

But. It's purely Windows and .NET.

In the last few years I have migrated my private life over to the Mac. My only regret is not making the move sooner, Macs are just the most amazing machines.
Being a developer I am obviously interested in learning about Mac-development using Cocoa, Objective C and Xcode, but I have had a hard time finding the time to really get into it. I hope starting this blog to keep a journal of my progress will help push me along.

Obviously, since I have plenty of experience of C# and Java I will look into Mac development using those languages as well and type up my experiences right here. Hopefully it will all add up to a collection of interesting tips and thoughts on software development on the Mac.

I am keeping this blog mostly for my own sake, but feel free to drop by to read my musings, and do drop me a comment with your best Mac tips and tricks. 

Oh yeah, I do have another regret; not buying huge amounts of Apple stock once I realized how amazing Apple's products really are.