我正在构建一个 iPhone 应用程序,我在其中分离一些线程以在后台执行长时间运行的工作,以免挂起 UI。我知道线程需要 NSAutoreleasePool 实例来进行内存管理。我不确定线程方法是否调用另一个方法 - 该方法是否还需要 NSAutoreleasePool?
示例代码:
- (void)primaryMethod {
[self performSelectorInBackground:@selector(threadedMethod) withObject:nil];
}
- (void)threadedMethod {
NSAutoreleasePool *aPool = [[NSAutoreleasePool alloc] init];
// Some code here
[self anotherMethod];
// Maybe more code here
[aPool drain];
}
- (void)anotherMethod {
// More code here
}
我问的原因是我收到的错误是对象正在自动释放而没有适当的池,并且“只是泄漏”。
我已经看到人们根本没有自动释放池的其他问题,我理解为什么需要自动释放池。我特别想知道在(在本例中)创建的自动释放池是否threadedMethod
适用于在anotherMethod
.