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 application, 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:
This does not retain the array since you are accessing the ivar directly, not the property. You should do this instead:
Some programmers prefer to synthesize their properties with a different ivar name to avoid such mistakes.