我有内存泄漏吗?
我已经以编程方式创建了 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;
复制数组项并保留每个数组项,那么第三个输出应该是 b 2 对吗?
我有什么错吗???
I've created UITabBarController programmatically, like this
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]);
Also releasing mTabBarController in dealloc, like this,
- (void)dealloc {
[mTabBarController release];
...
}
Now the question : The output for the first code snippet is
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
Am I getting memory leak ? And why it prints 1 always ?
if it retains tabBarItems then the second output should be 2. If
mTabBarController.viewControllers = tabBarItems;
copies array items and retains each each array item then, the 3rd output should b 2 right ?
Do I get something wrong ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
保留计数错误:何时使用 -retainCount?
简而言之:您无法保证
retainCount
将返回一个合理的值。Retain count bad: When to use -retainCount?
In short: You can't guarantee
retainCount
will return a sensible value.