0

我正在编写一个仅在 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?

请问有什么帮助吗?

4

2 回答 2

4

不要使用 -retainCount。

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

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

有关完整的详细信息,请参阅内存管理指南


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

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

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

于 2010-11-26T20:38:21.987 回答
0

你释放UIPopoverController? 也许该对象仍在保留表格视图。

于 2010-11-27T00:39:17.053 回答