EXC_BAD_ACCESS error on a UITableView with ARC?
- by Arnaud Drizard
I am building a simple app with a custom bar tab which loads the content of the ViewController from a UITableView located in another ViewController.
However, every time I try to scroll on the tableview, I get an exc_bad_access error. I enabled NSzombies and guard malloc to get more info on the issue.
In the console I get:
"message sent to deallocated instance 0x19182f20"
and after profiling I get:
# Address Category Event Type RefCt Timestamp Size Responsible Library Responsible Caller
56 0x19182f20 FirstTabBarViewController Zombie -1 00:16.613.309 0 UIKit -[UIScrollView(UIScrollViewInternal) _scrollViewWillBeginDragging]
Here is a bit of the code of the ViewController in which the error occurs:
.h file:
#import <UIKit/UIKit.h>
#import "DataController.h"
@interface FirstTabBarViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView* tabBarTable;
}
@property (strong, nonatomic) IBOutlet UIView *mainView;
@property (strong, nonatomic) IBOutlet UITableView *tabBarTable;
@property (nonatomic, strong) DataController *messageDataController;
@end
.m file:
#import "FirstTabBarViewController.h"
#import "DataController.h"
@interface FirstTabBarViewController ()
@end
@implementation FirstTabBarViewController
@synthesize tabBarTable=_tabBarTable;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)awakeFromNib
{
[super awakeFromNib];
self.messageDataController=[[DataController alloc] init];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.messageDataController countOfList];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"mainCell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
};
NSString *expenseAtIndex = [self.messageDataController
objectInListAtIndex:indexPath.row];
[[cell textLabel] setText:expenseAtIndex];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return NO;
}
@end
This FirstTabBarViewController is loaded in the MainViewController with the following custom segue:
#import "customTabBarSegue.h"
#import "MainViewController.h"
@implementation customTabBarSegue
-(void) perform {
MainViewController *src= (MainViewController *) [self sourceViewController];
UIViewController *dst=(UIViewController *)[self destinationViewController];
for (UIView *view in src.placeholderView.subviews){
[view removeFromSuperview];
}
src.currentViewController =dst;
[src.placeholderView addSubview:dst.view];
}
@end
The Datacontroller class is just a simple NSMutableArray containing strings.
I am using ARC so I don't understand where the memory management error comes from. Does anybody have a clue?
Any help much appreciated ;)
Thanks!!