2010-10-20 141 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