Objective-c 发布被调用了这么多次
Posted
技术标签:
【中文标题】Objective-c 发布被调用了这么多次【英文标题】:Objective-c release called so many times 【发布时间】:2013-04-09 22:56:00 【问题描述】:我的根视图控制器的release方法被调用了38次,但我没有调用release,为什么会出现?
@implementation AppDelegate
- (void)dealloc
[_window release];
[_viewController release];
[super dealloc];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
else
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
@interface ViewController ()
@end
@implementation ViewController
-(oneway void)release
NSLog(@"release called");
[super release];
@end
输出
2013-04-09 19:47:36.060 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.063 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.064 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.065 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.065 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.066 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.067 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.068 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.072 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.073 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.074 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.075 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.077 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.080 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.081 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.081 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.082 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.108 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.110 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.111 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.112 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.134 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.144 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.158 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.161 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.170 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.180 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.182 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.193 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.197 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.198 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.199 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.202 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.208 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.208 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.218 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.220 TesteRelease[44985:c07] release called
2013-04-09 19:47:36.220 TesteRelease[44985:c07] release called
我没有使用 ARC。
【问题讨论】:
您的视图控制器对象可以被多个框架对象传递。此外,即使你不使用 ARC,Apple 也可能使用;并且 ARC 在调用保留/释放方面并不轻松。无论如何,我认为这与记录发布调用无关。仅当调用dealloc
时才有意义。
【参考方案1】:
框架的一部分在他们认为合适的时候保留和释放(或自动释放)对象。这是鼓励您不要尝试精确跟踪保留计数的众多原因之一——您不需要知道所有这些保留和释放正在发生,因为它们与您无关。
【讨论】:
【参考方案2】:您的视图控制器很可能会被添加到自动释放池中,因为它是通过系统库传递的。但只要您确信您的代码正确地管理视图控制器,您就应该将其视为背景噪音而忽略。
【讨论】:
以上是关于Objective-c 发布被调用了这么多次的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Swift 调用 Objective-C 类的工厂方法?
Swift 类使用 Objective-C 类使用 Swift 类
使用 XCTestCase Objective-C 进行多次测试性能