应用程序因错误而崩溃:修改正在最终确定的层

发布于 2024-12-02 21:25:15 字数 1468 浏览 0 评论 0原文

滚动表格视图后,我的应用程序崩溃并显示消息“正在修改正在最终确定的图层”。

我相信该错误是由于我在方法末尾释放了“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜光 2024-12-09 21:25:15

该错误是由于以下行引起的:

if([self.webViewCache objectForKey:cellid])
{
    videoView=[self.webViewCache objectForKey:cellid];
}

Here you are just getting the web view from adictionary,它显然返回一个 autoreleased 对象。因此,当您尝试释放它(不知道它是已分配还是刚刚从字典中获取)时,就会发生错误。

一种解决方案是保留 videoView

videoView=[[self.webViewCache objectForKey:cellid] retain];

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];
七色彩虹 2024-12-09 21:25:15
  1. 您正在分配

    UIWebView *videoView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 104, 104)];
    
  2. 然后,您可以将此引用替换为

    中的其他内容

    videoView=[self.webViewCache objectForKey:cellid];
    
  3. Then 中的其他内容

    [videoView发布];
    

永远不知道你是否要释放在#1 或#2 中获取的内存时。如果是#2,您可能会在后续调用中过度释放。

EmptyStack 解决方案可能可以解决您的问题。实现它时,还要注意释放您在第 1 点所做的分配。

  1. You are allocating

    UIWebView *videoView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 104, 104)];
    
  2. Then, you may replace this reference to something else from

    videoView=[self.webViewCache objectForKey:cellid];
    
  3. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文