I’ve run into a few problems during my short time developing for iPhone. Luckily the problems have not been with the iPhone device itself or the SDK, but mostly the tools; specifically the simulator. One of the more annoying problems occurs while running the Simulator. When launching an application a bad memory access is thrown and results in a “BAD_ACCESS” error message in the console. Most of the time is happens for what seems like totally random reasons.
Always it is while running a release build on the simulator. Obviously release mode on the simulator isn’t very useful for anything; however I am a bit anal and like to keep it compiling on any platform I can. I feel like if I have a problem on the simulator then it might be hinting at an underlying problem elsewhere. But I am beginning to believe the simulator is just a bit too buggy to care about.
The BAD_ACCESS always occurs right after the autorelease pool is created and the application is launched via a call to UIApplicationMain. I haven’t tracked down the actually cause but have come up with some strange workarounds to get it to go away.
#1 Mixed Thumb Compiling: One of the more difficult causes to track down was a case when I had the executable linking to several libraries, and the libraries had mixed Thumb Mode settings. The application and most of the libraries were compiled without Thumb support. However, on a single library I hadn’t disabled Thumb mode. When I disabled thumb mode on that library and recompiled, the BAD_ACCESS then went away. However, I don’t know if the mixed thumb mode setting was the actual cause of the problem, because it seems like changing just about anything can cure the BAD_ACCESS.
#2 Change Something… Anything: After fixing the BAD_ACCESS several times over a month using several different fixes, I ended up in a situation that I couldn’t resolve quickly. So I started searching the Internet to see if I could find a solution. That is when I ran across this Cocoa Nut post. It sounded like it might help me track down the root cause. However, when I set the NSZombieEnabled variable and relaunch the application I no longer received any BAD_ACCESS.
But that is not the strange part. I kept playing around with this and realized if I added any environment variable at all, then the BAD_ACCESS went away. The NSZombieEnabled variable didn’t matter. For example, I made the variable name ‘ThisVariableIsLying’ and set the value to ‘FALSE’ and when I relaunched the app the BAD_ACCESS did not occur. Removing the variable caused it to come back.
Anyway, I thought I would mention it as perhaps it might be useful to someone else, or maybe someone could shine some light on what might actually be going on.
Till next time,
Good luck.
Tags: Development, iPhone, software, XCode