My app is crashing with the message “modifying layer that is being finalized” after scrolling through the tableview.
I believe the error is due to the fact that I released ‘videoView’ (second last line of code) at the end of the method.
How can I resolve this issue?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* PlaceholderCellIdentifier = @"PlaceholderCell";
GenericObject *youTubeVid = [self.searchResultArray objectAtIndex:indexPath.row];
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:PlaceholderCellIdentifier]autorelease];
}
UIWebView *videoView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 104, 104)];
NSString *cellid=[NSString stringWithFormat:@"Cell%i%i", indexPath.section, indexPath.row];
if([self.webViewCache objectForKey:cellid])
{
videoView=[self.webViewCache objectForKey:cellid];
}
else
{
NSString *url = [NSString stringWithFormat:@"http://www.youtube.com/watch?v=%@",youTubeVid.vid];
NSString *videoHTML = [self embedYouTube:url frame:CGRectMake(0, 0, 104, 104)];
[videoView loadHTMLString:videoHTML baseURL:nil];
[self.webViewCache setObject:videoView forKey:cellid]; //Save webview in dictionary
}
[cell.contentView addSubview:videoView];
//Error seems to be here
[videoView release];
return cell;
}
The error is because of the line,
Here you are just getting the web view from a dictionary, which obviously returns an
autoreleasedobject. So, when you try to release it (without knowing whether it has been allocated or just got from dictionary) the error occurs.One solution would be to
retainthevideoView.