2010-10-20 81 views
1

我的applicationDidFinishLaunching我打电话:此代码是否会泄漏内存?

[self performSelectorInBackground:@selector(performReachabilityCheck) withObject:nil];

这里是performReachabilityCheck

-(void)performReachabilityCheck{ 
    internetReach = [[Reachability reachabilityForInternetConnection] retain]; 
    [internetReach startNotifer]; 
    [self updateInterfaceWithReachability: internetReach]; 
} 

我需要创建一个自动释放池?如果是这样,在这种情况下我该怎么做?

更新: 这是实现自动发布池的正确方法吗?

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
    [self performSelectorInBackground:@selector(performReachabilityCheck) withObject:nil]; 
    [pool release]; pool = nil; 

回答

1

是的,任何时候在后台线程上执行选择器时,都需要将其包装在AutoreleasePool中。您正在使用的类可能会创建自动释放对象。如果你在连接到调试器的时候运行这个程序,你应该会看到很多关于“没有autorelease池到位,只是泄漏”的消息。

+0

检查我更新的代码。我是否正确创建了自动发布池? – 2010-10-20 11:22:45

+1

@Sheehan,你必须把NSAutoreleasePool放到你的performReachabilityCheck方法中(在后台运行) – Vladimir 2010-10-20 11:26:57

相关问题