应用程序因错误而崩溃:修改正在最终确定的层
滚动表格视图后,我的应用程序崩溃并显示消息“正在修改正在最终确定的图层”。
我相信该错误是由于我在方法末尾释放了“videoView”(倒数第二行代码)造成的。
我该如何解决这个问题?
- (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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该错误是由于以下行引起的:
Here you are just getting the web view from adictionary,它显然返回一个
autoreleased
对象。因此,当您尝试释放它(不知道它是已分配还是刚刚从字典中获取)时,就会发生错误。一种解决方案是
保留
videoView
。The error is because of the line,
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
thevideoView
.您正在分配
然后,您可以将此引用替换为
中的其他内容
Then 中的其他内容
永远不知道你是否要释放在#1 或#2 中获取的内存时。如果是#2,您可能会在后续调用中过度释放。
EmptyStack 解决方案可能可以解决您的问题。实现它时,还要注意释放您在第 1 点所做的分配。
You are allocating
Then, you may replace this reference to something else from
Then when you
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.