Pages

Monday, July 26, 2010

EXC_BAD_ACCESS

Yesterday, I was so happy to have finally implemented both the bullets and the collision detection system. The performance was really good that it can even show up to 100 sprites on the screen at the same time - and all of this is happening with collision detection enabled! I was ready to tackle the next feature in my list - which is making the enemy drop items - but that is until our game crashed. I tried to relaunch it again, thinking that it might just be Steve Jobs' fault.. but then it happened again - only this time, it happened after 2 minutes of waiting. Hmm.. I tried to view the console - and there it was: EXC_BAD_ACCESS. I also tried to view the stack trace to see where the error has originated but to my luck, the stack trace was already gone at that point.

Being a new Objective-C developer, I am still not used to it's style of managing the memory although I am aware of how they work and what are the pitfalls that needs to be avoided. But then, it's bound to happen sooner or later and fortunately for me, I found the problem out while our game is still at it's infancy - where there's not much class hierarchy yet. And so I did my research - and according to my research, here are the main reasons for you to get this error:

1. Releasing an object that is already released (this mostly means that you do not release the objects that you are not directly responsible for releasing - unless you have retained it).
2. Accessing an object that is already released (this mostly means that if you'll be working with an object that's passed via a parameter outside of the call stack where it was delivered, then you'd want to retain it).
3. Adding or removing objects from an array that is currently being iterated (ehem..)

Nevertheless, EXC_BAD_ACCESS is that kind of error that doesn't give you a lot of clue as to how and where it happened. All that it gives you is the error name - and a lot of headache. As it was for me, I spent nearly 4 hours trying to debug this single problem only to find out that I was modifying the array that I was currently iterating! After tweaking the code a bit, our game is finally EXC_BAD_ACCESS free! Yay! So yeah, I'm happy that I was able to move past that and move on to the next item in my list.

Cheers,
Jim

P.S. There is an article that I found on the net on how to debug this error:
http://www.codza.com/how-to-debug-exc_bad_access-on-iphone

but for some reason, it didn't work for me. Oh well - still good to know just in case I get this problem again.

No comments:

Post a Comment