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."

Retro gaming on iPad, for iOS developers

 MADTV and Gabriel Knight running in dospad on the iPad

MADTV and Gabriel Knight running in dospad on the iPad

Sometimes it's good to take a break from coding, and what's more relaxing than running some retro games on your iPad?

Getting DOS onto iPad

Some time ago a DOS emulator called iDOS briefly made it into the App Store, but it was already long gone when I heard of it and tried to download it. As luck would have it the source code for the app is available under the name dospad from Google Code, so any registered iOS developer can build it using Xcode and run it on their iPad.

I believe iDOS or dospad is also available for jailbroken iPads, for those who are not registered developers but are OK with jailbreaking their device.

Installing games

With dospad you get a fully functional DOS system for your iPad. There is mouse support and sound support, making it brilliant for some retro gaming. If the games are mouse driven you can even go full screen for a very immersive experience.

Sierra's adventure game Gabriel Knight is one of my all time favorites, and since I no longer have a floppy disk drive on my computer I downloaded it from an abandonware site called The House of Games, where there's a large selection of old DOS games.

dospad2.png

With dospad installed on your iPad, you can drag files into it using iTunes. Just go to the iPad's Apps tab and select dospad under the File Sharing header.

Dospad comes with an unzip utility so once you have transferred the zip file with your game you can use the DOS command prompt to create a directory and unzip the file into it.

dospad3.png

For Sierra games you then run install.exe to select your sound options. I selected Soundblaster Pro which seems to work well.

Launch the game by running sierra.exe.

Mouse controls

Obviously, this being DOS, you don't use it like a standard touch screen. Instead you control a mouse cursor on screen in the same manner you would using the touchpad on your Macbook. Tapping the screen left clicks at the position the cursor points to.

dospad4.png

You can play either in portrait or landscape mode, as well as full screen.

dospad5.png

Happy retro gaming, and make sure to save your game often.

iPhone Video Output

There are a couple of options available when preparing to perform a demonstration of an iPhone app to a larger group of people. All these options have flaws, however. The "gather around" method, for example, doesn't work well for groups of more than a few people. Any more and you would ideally want to project the iPhone screen contents onto a big screen.


The "iPhone simulator method" is probably the most used method. It's simple to set up but limits your app navigation to the mouse, making multitouch gestures difficult to perform. Also, there is no way to show off accelerometer functionality like "shake to undo".
The "camcorder method" avoids the simulator's issues, but can be a hassle to set up. It also restricts your movement as you have to make sure to keep the device on screen and avoid reflections as best you can.

Doing it Steve's way
Why don't we take a page out of Steve Job's playbook and mirror the iPhone screen directly from the device onto the big screen?
Sadly this is not straight forward. So far only the iPod application supports video output via Apple's AV cable, at least that's what I thought until I stumbled upon Rob Terrell's iPhone App Video Mirroring blog post (go read it now, I'll wait).

Using the private MPTVOutWindow class in the MediaPlayer.framework API, Rob's code mirrors the iPhone screen onto the AV cable. For demonstration purposes this is a great solution, but being based on a private API it should not be left in the app when submitting it to the App Store.

Originally the code did not support on-the-fly orientation changes and touch indicators, both of which I needed. Thanks to Rob posting the source code I was able to implement these changes and have since submitted the updated code back to him, although I haven't heard back after doing so.

Get your hands on the code
In the spirit of sharing I have prepared a sample project, TvOutputSample, which shows you how to add video output to an Xcode project. This is the application shown in the video above, it should build and run out of the box, using iPhone SDK 3.0.

Download the code: TvOutSample.zip
[UPDATE: The project has moved to github]

I should mention that since the video mirroring is software based it affects application performance somewhat. On an original iPhone 10 fps works well, on an iPhone 3GS I have had no problems running at 20 fps. The fps setting is near the top of the UIApplication_TVOut.m file, do your own tests to see what works best for your app and hardware.

The current version (as of October 2009) of the code does not support OpenGL video output.

 

iPhone SDK development on multiple computers

Sometimes it's good to be able to use several computers to develop your iPhone app. In my case my main development machine is the iMac, but summer is coming up and I may not want to stop developing just because I go out of town. Luckily I have a MacBook, on which I have also installed the iPhone SDK.

In order to test on the device when I develop using the MacBook I have to move my certificate, private key and provisioning profile to it. Here's how I do that.

1. Launch Keychain Access on the iMac (main development computer).

2. Under the Keys category I Ctrl-click the private key that has the certificate for 'iPhone Developer: ' attached to it.

3. In the context menu select 'Export ...'.

4. When saving provide a password, which will be required for importing on the other computer.

5. A .p12 file was saved, transfer it to the target computer.

6. Grab the development provisioning profile (either by downloading from the iPhone Program Portal or by grabbing the right one from ~/Library/MobileDevice/Provisioning Profiles/) and transfer it to the target computer.

7. Double click the .p12 file on the target computer. If you provide the correct password the key and certificate will be installed into the Keychain on the target computer.

8. Drag the provisioning profile onto the Xcode dock icon.

The application can now be installed on the device from the target computer, which in my case is the lovely black MacBook.

A few thoughts about the Cocoa Bootcamp

This fall I attended Aaron Hillegass' Cocoa Bootcamp at Big Nerd Ranch. I had planned to write up my thoughts about the experience earlier, but life (and work) interrupted.

After being confirmed for the class I made travel arrangements to arrive in Atlanta one day early to adjust to the time difference, and also to get a chance to see a little of Atlanta itself. I ended up having time to both visit the Apple Store at Lenox Square Mall and visiting the Georgia Dome for a taste of the NFL (the Broncos visiting the Falcons). I'm really happy I made the decision to fly in early, as I had a very good time in town.

 NFL action

NFL action

On Sunday I ventured back to the airport via the subway to be picked up by the Big Nerd Ranch shuttle. While waiting at the baggage claim area I met a few other attendees and then the shuttle came by to pick us all up for transport to The Historic Banning Mills, where dinner was waiting for us. We were provided with a nice room each, overlooking a beautiful valley.

 The Historic Banning Mills

The Historic Banning Mills

 Spectacular views all around

Spectacular views all around

The next morning the Bootcamp got going, and within minutes Aaron had us coding our first app. The class followed Aaron's book Cocoa Programming for Mac OS X, with Aaron explaining some concept from each chapter and then having us using the learned concepts in code. There were also a couple of things covered which aren't in the book, for example an iPhone project. The frequent coding assignments and the opportunity to raise any questions with Aaron or any of the other students was a very efficient way of learning.

The pace in the classroom was intense, but every afternoon also provided the chance for a brief walk around the beautiful scenery, with Aaron himself as guide. This was a good opportunity to catch your breath, think about what you had just learned in a broader perspective, and to take a few photos.

 Aaron goes for a walk

Aaron goes for a walk

Every day we were provided with no less than three huge and delicious meals, I think I ate more good food during the Cocoa Bootcamp than I will during Christmas, which says a lot. There were also snacks and soft drinks provided in the classroom, so no risk of going without energy. A good thing, since it did take quite a bit of energy keeping up with the learning tempo.

I think it would probably have been possible to learn pretty much what the class teaches by yourself by spending a couple of weeks or months with the book. I tried to go down that route this summer, but kept getting side tracked. Nevertheless I must reiterate - the book is a must buy for any newcomer to the platform. 

I'm really glad I took the class since it propelled me right past the first plateau in the learning curve and I now feel comfortable using Xcode and the Interface Builder. The opportunity to ask questions and have Aaron and others help debugging problems encountered during the assignments totally eliminated the frustrations encountered when trying to learn from reading a book on your own. The distraction free surroundings also helped me focus my mind on the task at hand.

I would say the Cocoa Bootcamp is probably the perfect way to get started with Cocoa development for the Mac. I'm happy, though, that I asked my employer to cover the costs when negotiating my contract for employment, as it is not exactly cheap. Nevertheless, if you can afford it I think that the Cocoa Bootcamp provides good value for the money spent.

A further advantage was that I got to meet a great, and diverse, group of people dedicated to software development and the Mac platform. The attendants were from all over the US, even some local to the area, as well as a couple of us Europeans.

If you attend the Cocoa Bootcamp, you'll leave having the confidence and understanding necessary to take on true Cocoa development projects. Let's hope something like that comes my way eventually, even though my day job is currently strictly .NET.

I'll make good use of my newfound knowledge in my spare time either way. Stay tuned...

Running SDK apps on a jailbroken 2.0.1 iPhone [Updated]

Update: There is useful information in the comments about how to do this with later versions of the SDK as well. Thanks to everyone who has contributed. 

 

This summer I've spent some time working on my iPhone app, which I hope to release on the App Store later this fall.

Since I imported my iPhone from the USA, I have to use the iPhone Dev Team's Pwnage Tool to unlock and jailbreak it. I chose to stay on firmware 1.1.4 until 2.0.1 was available to avoid the performance issues that were widely reported. This means I have not had the opportunity to try my application on the phone itself, but last night I updated to 2.0.1 and tried installing the app.

It turns out my application works great, but I still need to add a few more features and spend some time generating nice visuals to replace the placeholder graphics I am using.

To get the application running on the iPhone I had to copy it using scp to the /Applications directory on the phone and bypass the iPhone Code Signature check. After some googling I found the ldid tool written by Jay Freeman, which takes care of the Code Signature.

Here are the steps I used to get my SDK developed app running on a jailbroken iPhone, all commands should be entered as one liners in the Terminal:

1. Make sure you set Xcode to compile for the device, not for the simulator.

2. Compile the project, then copy the resulting MyApp.app directory to the iPhone's /Applications directory.

mac# scp -r MyApp.app/ root@iphone_ip:/Applications/

3. Access the phone using SSH. The root password is alpine, at least on my phone.

mac# ssh root@iphone_ip 

4. Make sure the executable is marked as such, this step is probably unneccessary but it wont hurt anything.

iphone# chmod +x /Applications/MyApp.app/MyApp

5. Install the ldid tool, this step probably needs Cydia to be installed on your phone.

iphone# apt-get install ldid

6. Run ldid on the application executable on the phone.

iphone# ldid -S /Applications/MyApp.app/MyApp

7. Your application icon should now turn up once the phone is rebooted.

These steps will work even if you have not been accepted to the paid iPhone developer program.

Note that these instructions are experimental. I can't be held responsible for whatever happens if you try them.