aspalding
aspalding

Reputation: 151

Problems with tableView and deallocated instance

Creating a simple RSS feed reader with the master-view template provided by XCode (4.6).

The application loads the first screen full of titles, then when scrolling down or up, it bugs out with an error.

The error I'm receiving in the console is:

[__NSArrayM retain]: message sent to deallocated instance 0x7522d40

The trace is leading me to this line in my cellForRowAtIndex function. XCode marks this line as signal SIGKILL. (This may because I set zombie objects as per instruction from an tutorial on debugging).

NSMutableDictionary * news = (NSMutableDictionary *)[self.nf.newsStories objectAtIndex:indexPath.row];

Here is the rest of that function.

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

NSString * cellId = @"feeds";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellId];

if(cell == nil){
    cell = [[UITableViewCell alloc] init];
    NSMutableDictionary * news = (NSMutableDictionary *)[self.nf.newsStories objectAtIndex:indexPath.row];
    [cell.textLabel setText:[news objectForKey:@"title"]];
}

return cell;
}

Here is my viewDidLoad function.

- (void)viewDidLoad
{
[super viewDidLoad];
self.nf = [[NewsFeed alloc]init];
[self.nf setFeedURL:[NSURL URLWithString:@"http://rss.cnn.com/rss/cnn_topstories.rss"]];
[self.nf retrieveFromInternet];

//For debugging. 
for(id newsItem in self.nf.newsStories){
    NSDictionary * d = (NSDictionary *)newsItem;
    NSLog(@"Title: %@\nDescription: %@", [d objectForKey:@"title"], [d objectForKey:@"description"]);
}

}

Any help would be appreciated. Thanks guys.

Upvotes: 3

Views: 93

Answers (1)

tdh1989
tdh1989

Reputation: 216

For some reason I can't add comments, but you should move

NSMutableDictionary * news = (NSMutableDictionary *)[self.nf.newsStories objectAtIndex:indexPath.row];
    [cell.textLabel setText:[news objectForKey:@"title"]];

out of the conditional... and I'm not sure why you're making it mutable. Also, change the cell initiation to:

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellId];

Other than that, if you are using ARC make sure your array in your newsfeed has property set to strong.

Upvotes: 2

Related Questions