【问题标题】:I'm having five allocations for an object with only one alloc in the same function我在同一个函数中只有一个分配的对象有五个分配
【发布时间】:2010-11-26 11:02:53
【问题描述】:

我正在编写一个仅在 popOver 中显示菜单(由 tableView 组成)的函数。

这是源代码:

-(void)pushSearch:(NSString *)option type:(int)optionType
{
    searchNav = [[iNavigation alloc] initWithNibName:@"iNavigation" bundle:nil] ;
    //This is the UIViewController with the tableView

    [searchNav setSearchMode:optionType];

    searchNav.view.frame =CGRectMake(0,0,300,600);

    NSLog(@"Retain Count: %d",[searchNav retainCount]);

//此时retain count为1

    if ([pop isPopoverVisible])
    {
        [pop dismissPopoverAnimated:YES];
        [pop release];
    }

    pop = [[UIPopoverController alloc] initWithContentViewController:searchNav];

    NSLog(@"Retain Count: %d",[searchNav retainCount]);
    //At this point retain count is 2



    [pop presentPopoverFromRect:btnMenu.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    [pop setPopoverContentSize:CGSizeMake(350,600)];

    NSLog(@"Retain Count: %d",[searchNav retainCount]);
    //At this point retain count is 5!!!

    [searchNav release];

}

我遇到的问题是用于加载 tableview 的内存永远不会被释放。我的应用程序在内存中不断增长,直到崩溃。

为什么如果我只为 searchNav 分配一个,在将它分配给 popOver 后,retin 计数是 5?

请帮忙?

【问题讨论】:

    标签: iphone objective-c uitableview retaincount


    【解决方案1】:

    不要使用 -retainCount。

    对象的绝对保留计数是没有意义的。

    您应该调用release 的次数与您导致对象被保留的次数完全相同。不会少(除非您喜欢泄漏),当然也不会更多(除非您喜欢崩溃)。

    详情请参阅Memory Management Guidelines


    保留计数为 5 是无关紧要的,很可能是因为各种框架的内部实现细节。

    该代码中searchNav的内存管理是正确的;您分配了对象(+1 保留),然后最终释放了它(-1 保留)。因此,内存泄漏在其他地方。

    尝试在您的源代码上使用“构建和分析”。然后使用 Allocations 工具查看随着您的应用程序随时间增长而悬空的对象。某处存在过度保留;您尚未将其与发布平衡的保留。

    【讨论】:

    • 我们可以让[retaincount]标签自动重定向到[retaincount-is-evil-stopit-stopit-stop]吗?
    【解决方案2】:

    你发布了UIPopoverController 吗?也许那个对象仍然保留着表格视图。

    【讨论】:

      猜你喜欢
      • 2012-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-15
      • 2020-08-25
      • 1970-01-01
      • 1970-01-01
      • 2018-10-29
      相关资源
      最近更新 更多