I'm now been stuck with this error "EXC_BAD_ACCESS" for hours and I can't seem to find what's wrong. Hopefully some of you will find it.
I'm downloadning a file containing json-data, save it as a custom NSObject in an NSMutableArray and then present it in a UITableView. It's when I going to present it in the UITableView I encounter problems.
ViewController.h
@property (nonatomic, retain) NSMutableArray *menuItems;
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
menuItems = [[NSMutableArray alloc] init];
}
- (void)viewDidAppear:(BOOL)animated
{
[self downloadMenu];
}
-(void)downloadMenu
{
dispatch_queue_t fetchfromServerQueue = dispatch_queue_create("fetchfromServer", NULL);
[self createProgressionAlertWithMessage:@"Fetching menu"];
dispatch_async(fetchfromServerQueue, ^{
NSURL *menuURL = [NSURL URLWithString:@"anURL"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:menuURL];
NSError *error = nil;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
responseArray = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
menuItems = [[NSMutableArray alloc] initWithCapacity:[responseArray count]];
for (int i = 0; i<[responseArray count]; i++) {
NSDictionary *menuObject = [responseArray objectAtIndex:i];
NSString *x = [NSString stringWithFormat:@"%@", [menuObject objectForKey:@"x"]];
NSString *y = [NSString stringWithFormat:@"%@", [menuObject objectForKey:@"y"]];
NSString *z = [NSString stringWithFormat:@"%@", [menuObject objectForKey:@"z"]];
MenuItem *menuItem = [[MenuItem alloc] initWithX:x andY:y andZ:z];
[menuItems addObject:menuItem];
[menuItem release];
}
dispatch_async(dispatch_get_main_queue(), ^{
[progressAlert dismissWithClickedButtonIndex:0 animated:YES];
[menuTable reloadData];
});
});
dispatch_release(fetchfromServerQueue);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [menuItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
MenuItem *menuItem = (MenuItem *)[menuItems objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@", [menuItem x]];
return cell;
}
On "cell.textLabel.text = [NSString stringWithFormat:@"%@", [menuItem x]];" I get "EXC_BAD_ACCESS (code=1,address=0xXXXXXXXX)".
I don't use ARC and can't convert to it in a near future with this project. That make it a problem due to my lack of knowledge with memory management.
Any idea of what's wrong?
Thx!
#### #### #### #### #### #### #### #### ####This is how MenuItem looks like:
#import <Foundation/Foundation.h>
@interface MenuItem : NSObject
{
NSString *x;
NSString *y;
NSString *z;
}
-(id)initWithX:(NSString *)x_ andY:(NSString *)y_ andZ:(NSString *)z_;
-(NSString *)x;
-(NSString *)y;
-(NSString *)z;
@property(retain, nonatomic) NSString *x;
@property(retain, nonatomic) NSString *y;
@property(retain, nonatomic) NSString *z;
@end
#import "MenuItem.h"
@implementation MenuItem
@synthesize x;
@synthesize y;
@synthesize z;
-(id)initWithX:(NSString *)x_ andY:(NSString *)y_ andZ:(NSString *)z_
{
self = [super init];
if(self)
{
x = x_;
y = y_;
z = z_;
}
return self;
}
-(void)dealloc
{
[x release];
[y release];
[z release];
[super dealloc];
}
-(NSString *)x
{
return x;
}
-(NSString *)y
{
return y;
}
-(NSString *)z
{
return z;
}
@end
I don't see anything obvious, perhaps something in menuItem
is causing the issue? What does [menuItem x]
do?
In general, run the app with the zombies instrument: cmd-i, then select Zombies. This will give you a trace of the allocations/releases of the object which is throwing the EXC_BAD_ACCESS.
Good luck.