EXC_BAD_ACCESS accessing an NSArray on Delegate app
I am developing an iPhone app and I have a problem accessing to one NSArray defined on delegate.
My app has two tabs. These two tabs have an UITableView. I have the source of the data of these tables on one NSArray defined in the delegate.
When I load the appli开发者_运维技巧cation, the first tab loads correctly all the content. This is one part of code of the first tab controller:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
mainAppDelegate *delegate = [[UIApplication sharedApplication]delegate];
return [delegate.dataArray count];
}
When the debugger is on return statement, I can see that dataArray has 9 elements. The code works fine.
When I click on the second tab, the same code is executed in the second's tab controller. But while debugger is on return statement, I can see that delegate is not null and dataArray shows
{(int)[$VAR count]} objects
On next step, program crashes giving EXC_BAD_ACCESS
I think in this point dataArray has been released, but I don't know when. I have deleted dataArray release calls on dealloc functions.
This is the property definition of dataArray:
@property (nonatomic, retain) NSArray *dataArray;
Anyone has any idea? I'm completely lost on it. Thanks for your help!
As per your comment, you initialize your array like this:
dataArray = [dict objectForKey:@"data"];
This does not retain the array since you are accessing the ivar directly, not the property. You should do this instead:
self.dataArray = [dict objectForKey:@"data"];
Some programmers prefer to synthesize their properties with a different ivar name to avoid such mistakes.
@synthesize dataArray = dataArray_;
In your comments you just said that dataArray is the response of a servlet and you're parsing it with json-framework.
This sounds like your dataArray object just stores a reference to an external array object being passed in.
You haven't posted much code. Could you show where your dict object is initialized?
Your external object might be getting released causing an EXC_BAD_ACCESS
精彩评论