Friday, December 3, 2010

SIGABRT or EXC_BAD_ACCESS when accessing variables in AppDelegate

This is my first blog and hopefully it can help some of you out there in iPhone development.

I noticed a unknown error which crashes my app with SIGABRT or EXC_BAD_ACCESS. I don’t understand the problem but I have a solution for it.

For example, if you have an array in AppDelegate like
NSMutableArray *anArray;
You initialize and add an object to it (NSDictionary  in my case):
anArray = [[NSMutableArray alloc] init];
NSDictionary *aDict = [NSDictionary dictionaryWithObject:@”1” forKey:@”first”];
[anArray addObject:aDict];
Usually, I release the aDict here:
[aDict release];

At this point I realize that I did not allocate aDict and maybe I shoud not release it since it will autorelease. In my viewController, I can access anArray in viewWillAppear and everything will go smoothly.
But, when it is inside a function triggered through NSNotification, my app crashes as it tries to access that variable. The simplest solution is to not release aDict after it is added into anArray and everything will work fine.

If you have any idea on what is going on, let me know. Thanks!!!