2011-05-21 56 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