My app is crashing with the message "modifying layer that is being finalized" after scrolling through the table开发者_开发问答view.
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,
if([self.webViewCache objectForKey:cellid])
{
videoView=[self.webViewCache objectForKey:cellid];
}
Here you are just getting the web view from a dictionary, which obviously returns an autoreleased
object. 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 retain
the videoView
.
videoView=[[self.webViewCache objectForKey:cellid] retain];
You are allocating
UIWebView *videoView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 104, 104)];
Then, you may replace this reference to something else from
videoView=[self.webViewCache objectForKey:cellid];
Then when you
[videoView release];
you never know whether you are release the memory acquired in point #1 or #2. If it is #2, you might be end up over releasing in subsequent calls.
EmptyStack solution could be one that could solve your problem. When implementing it, also take care of releasing the allocation you made point #1.
精彩评论