Objective c n泄漏

Objective c n泄漏,objective-c,memory-leaks,nsthread,nsinvocation,Objective C,Memory Leaks,Nsthread,Nsinvocation,我正在尝试使用performSelectorInBackground设置一个nsinovication系统,以便在后台线程中启动选择器:-到目前为止,在实例方法(-)上运行系统时一切都是成功的,但我还希望支持类方法(+)。我已经调整了我的代码,为这两种类型的类提供了invokeInBackgroundThread,除了一个问题之外,所有的东西都正常工作。当类方法被调用时,我的控制台中充斥着“autoreleased with no pool in place”消息。不知道是什么引起的。下面显示了

我正在尝试使用performSelectorInBackground设置一个nsinovication系统,以便在后台线程中启动选择器:-到目前为止,在实例方法(-)上运行系统时一切都是成功的,但我还希望支持类方法(+)。我已经调整了我的代码,为这两种类型的类提供了invokeInBackgroundThread,除了一个问题之外,所有的东西都正常工作。当类方法被调用时,我的控制台中充斥着“autoreleased with no pool in place”消息。不知道是什么引起的。下面显示了基于DDFoundation开源项目的代码





默认情况下,主线程具有自动释放池,如果启动额外线程,则创建池是您的工作。实际上,这里没什么复杂的,只是

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
// Work...
[pool release];
另外,如果您有很多线程,我建议您查看NSOperation,而不是使用[performSelectorInBackground]运行线程。NSOperation(带包装队列)是此类任务更灵活的解决方案

...
- (void)forwardInvocation:(NSInvocation *)ioInvocation
{
    [ioInvocation setTarget:[self target]];
    [self setInvocation:ioInvocation];

 if (_waitUntilDone == NO) {
  [_invocation retainArguments];
 }

    if (_threadType == INVOCATION_MAIN_THREAD)
    {
        [_invocation performSelectorOnMainThread:@selector(invoke)
                                      withObject:nil
                                   waitUntilDone:_waitUntilDone];
    } else {
        [_invocation performSelectorInBackground:@selector(invoke)
                                  withObject:nil];
 }
}
...
+(void)doSomething;
[[className invokeOnBackgroundThread] doSomething];
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
// Work...
[pool release];