我有内存泄漏吗?

时间:2011-11-01 14:02:03

标签: ios memory-management memory-leaks ios4

我已经以编程方式创建了UITabBarController,就像这样

mTabBarController = [[UITabBarController alloc] init];
...
NSLog(@"The ref count is : %d", [tabBarItems retainCount]);
mTabBarController.viewControllers = tabBarItems;
NSLog(@"The ref count is : %d", [tabBarItems retainCount]);
[tabBarItems release];
NSLog(@"The ref count is : %d", [tabBarItems retainCount]);

同样在dealloc中发布mTabBarController,如下所示,

- (void)dealloc {
    [mTabBarController release];
    ...
}

现在的问题是:第一个代码段的输出是

2011-11-01 17:48:26.554 PostCardPrinter[12176:207] The ref count is : 1
2011-11-01 17:48:26.561 PostCardPrinter[12176:207] The ref count is : 1
2011-11-01 17:48:26.561 PostCardPrinter[12176:207] The ref count is : 1

我内存泄漏了吗?为什么它总是打印1?

如果它保留tabBarItems,那么第二个输出应该是2.如果

mTabBarController.viewControllers = tabBarItems;

复制数组项并保留每个数组项,然后第3个输出应该是b 2对吗?

我出错了吗?

1 个答案:

答案 0 :(得分:2)

保留计数错误:When to use -retainCount?

简而言之:您不能保证retainCount会返回合理的价值。