2013-05-06 82 views
0

內存管理,我現在已經堅持了這個錯誤「EXC_BAD_ACCESS」幾個小時,我似乎無法找出什麼是錯的。希望你們中的一些人能夠找到它。與GCD,NSMutableArray中和的UITableView

我downloadning含JSON-數據的文件,將其保存爲在一個NSMutableArray定製NSObject的,然後在一個UITableView呈現它。這是當我要在UITableView中呈現它時遇到問題。

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; 
} 

開 「cell.textLabel.text = [NSString的stringWithFormat:@」 %@ 「[菜單項X]];」我得到「EXC_BAD_ACCESS(code = 1,address = 0xXXXXXXXX)」。

我不使用ARC,並且不能與這個項目轉換爲它在不久的將來。由於缺乏對內存管理的知識,這使其成爲一個問題。

有什麼想法嗎?

Thx!

#### #### #### #### #### #### #### #### ####

這是菜單項的樣子:

#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 

回答

1

我沒有看到任何明顯的東西,或許是menuItem中的某些東西導致了這個問題? [menuItem x]做什麼?

一般而言,運行與殭屍儀器應用程式:CMD-I,然後選擇殭屍。這會給你一個拋出EXC_BAD_ACCESS的對象的分配/釋放的跟蹤。

祝你好運。

+0

我瘋狂的猜測:你的第一段是點亮,但第二段有點偏離。我打賭什麼'x'是,它不是一個對象。 – Chuck 2013-05-06 23:56:09

+0

我添加了MenuItem的代碼。 'x'是NSString。你覺得那個班有什麼問題嗎? – Cayak 2013-05-07 05:54:01

+0

今天晚些時候我會嘗試殭屍。 – Cayak 2013-05-07 05:54:34

0

這是一個有點怪異:

-(void)downloadMenu 
{ 
    dispatch_queue_t fetchfromServerQueue = dispatch_queue_create("fetchfromServer", NULL); 
    dispatch_async(fetchfromServerQueue, ^{ 
     ... 
    }); 
    dispatch_release(fetchfromServerQueue); 
} 

您創建一個隊列,並給它一個異步任務來完成。當這個任務開始做一些工作時,你立即釋放隊列。如果異步任務在您釋放之前未完成,這可能會導致一些問題。

您可能要推遲發佈,直到異步調用的工作完成之後。

+2

這不是問題;該塊保留隊列,直到它完成運行。見 「討論」 在[文檔用於'dispatch_queue_create()'](http://developer.apple.com/library/mac/documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html#//apple_ref/doc/ uid/TP40008079-CH2-SW14) – 2013-05-06 23:49:29

+0

不夠公平。我正在查看示例[oq](http://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html),他們將發佈放在回調中。 – Anonymous 2013-05-07 00:19:42