2011-05-21 60 views
1

這是又一個EXC_BAD_ACCESS問題。儘管我已經完成了作業,並確定我不會過度釋放我的NSArray。如何修復NSArray屬性上的EXC_BAD_ACCESS?

因此,這裏是我的代碼片段:

tableData = [NSDictionary dictionaryWithJSONString:JSONstring error:&error]; 
//Collect Information from JSON String into Dictionary. Value returns a mutli 
dimensional NSDictionary. Eg: { value => { value => "null"}, etc } 

NSMutableArray *t_info = [[NSMutableArray alloc] init]; 
for(id theKey in tableData) 
{ 
    NSDictionary *get = [tableData objectForKey:theKey]; 
    [t_info addObject:get]; 
    [get release]; 
} // converting into an NSArray for use in a UITableView 

NSLog(@"%@", t_info); 
//This returns an Array with the NSDictionary's as an Object in each row. Returns fine 

if (tvc == nil) 
{ 
    tvc = [[tableViewController alloc] init]; //Create Table Controller 
    tableView.delegate = tvc; 
    tableView.dataSource = tvc; 
    tvc.tableView = self.tableView; 
    tvc.tableData = t_info; //pass our data to the tvc class 
    [tvc.tableView reloadData]; 
} 
... 

現在在我的TableViewController類:

@implementation tableViewController 
@synthesize tableData, tableView; 

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [tableData count]; //Returns X Amount Fine. 
} 

- (UITableViewCell *)tableView:(UITableView *)the_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier"]; 

    UITableViewCell *cell = [the_tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
    } 

    NSLog(@"%@", tableData); //** CRASHES!!** 
    cell.textLabel.text = @"This is a test"; 
    return cell; 
} 

如果我註釋掉的NSLog,它會正常工作,並返回「這是對每個表格行的測試「。

這一個真的讓我難住,我對這個問題的所有文章通常都與保留/內存問題有關。

此外,另一個重要的一點。 如果我要從我的第一個類代碼中通過我的原始(NSDictionary)tableData並在我的tableViewController中運行相同的腳本 - 我可以非常好地NSLog對象。

回答

1

您需要釋放對象的唯一時間是如果您已通過new,alloccopy明確分配它。

NSMutableArray *t_info = [[NSMutableArray alloc] init]; 
for(id theKey in tableData) 
{ 
    NSDictionary *get = [tableData objectForKey:theKey]; 
    [t_info addObject:get]; 
    [get release]; 
} 

您不應該在這裏發佈get。通過這樣做,你可以釋放tableData字典持有的引用,這是不好的。我的猜測是,這是什麼導致你遇到的問題。

如果我沒有弄錯,[tableData count]返回期望值的原因是因爲數組仍然保留在已經發布的引用上。

+0

好吧,我會被詛咒!謝謝先生 – Moe 2011-05-21 07:23:15

+0

沒問題。在那裏,做到了,很高興我能夠幫助! – csano 2011-05-21 07:26:36