3

我在 NSOperationQueue 中运行 NSInvocationOperation 类型的操作,并且想知道自动释放对象是否安全 - 也就是说,是否保证为每个操作启动的线程都有自己的自动释放池。

我没有找到任何用于操作的文档自动释放池 - 阅读 Apple 的文档实际上表明我确实需要定义自己的自动释放池。

但是:1)我看不到仪器中的任何泄漏,至少不会超过我在操作中分配自己的自动释放池时。

2)查看调试器我可以看到这个堆栈跟踪:

#0  0x00fc3e82 in -[NSObject(NSObject) release] ()
#1  0x00faaa6c in CFRelease ()
#2  0x00fbf804 in __CFBasicHashDrain ()
#3  0x00faabcb in _CFRelease ()
#4  0x00fcfb8d in _CFAutoreleasePoolPop ()
#5  0x000edd0d in -[__NSOperationInternal start] ()
#6  0x000ed826 in ____startOperations_block_invoke_2 ()
#7  0x94358024 in _dispatch_call_block_and_release ()
#8  0x9434a2f2 in _dispatch_worker_thread2 ()
#9  0x94349d81 in _pthread_wqthread ()
#10 0x94349bc6 in start_wqthread ()

所以看起来好像有一个 CFAutoreleasePool - 假设这个对象会在操作完成时对我所有的自动释放对象调用释放是否安全?

4

1 回答 1

4

我写了一个小程序来测试是否NSInvocationOperation会为操作创建一个自动释放池:

#import <Foundation/Foundation.h>

@interface MyClass : NSObject
@end

@implementation MyClass
- (void)performSomeTask:(id)data
{
    NSString *s = [[[NSString alloc] initWithFormat:@"hey %@", data]
        autorelease];
    if ([[NSThread currentThread] isMainThread])
        NSLog(@"performSomeTask on the main thread!");
    else
        NSLog(@"performSomeTask NOT on the main thread!");

    NSLog(@"-- %@", s);
}
@end

int main(int argc, char *argv[]) {
  MyClass *c = [MyClass new];

  if (argc == 2 && strcmp(argv[1], "nop") == 0)
      [c performSomeTask:@"ho"];
  else {
      NSInvocationOperation *op = [[NSInvocationOperation alloc]
          initWithTarget:c
                selector:@selector(performSomeTask:)
                  object:@"howdy"];
      NSOperationQueue *queue  = [[NSOperationQueue alloc] init];
      [queue addOperation:op];
      [op waitUntilFinished];

      [op release];
      [queue release];
  }

  [c release];

  return 0;
}

它的工作原理如下:如果在命令行上传递“nop”,它将-performSomeTask:直接在主线程上执行,没有自动释放池。结果输出是:

$ ./c nop
*** __NSAutoreleaseNoPool(): Object 0x10010cca0 of class NSCFString autoreleased with no pool in place - just leaking
performSomeTask on the main thread!
-- hey ho

自动释放的字符串-performSomeTask:会导致泄漏。

在不传递“nop”的情况下运行程序将-performSomeTask:通过NSInvocationOperation另一个线程执行。结果输出是:

$ ./c
*** __NSAutoreleaseNoPool(): Object 0x100105ec0 of class NSInvocation autoreleased with no pool in place - just leaking
*** __NSAutoreleaseNoPool(): Object 0x100111300 of class NSCFSet autoreleased with no pool in place - just leaking
*** __NSAutoreleaseNoPool(): Object 0x100111b60 of class NSCFSet autoreleased with no pool in place - just leaking
*** __NSAutoreleaseNoPool(): Object 0x100105660 of class NSCFSet autoreleased with no pool in place - just leaking
performSomeTask NOT on the main thread!
-- hey howdy

正如我们所看到的,有 和 的实例NSInvocation正在NSSet泄漏,但其中的自动释放字符串-performSomeTask:没有泄漏,因此为该调用操作创建了一个自动释放池。

我认为可以安全地假设NSInvocationOperation(并且可能是 Apple 框架中的所有NSOperation子类)创建自己的自动释放池,就像并发编程指南建议的自定义NSOperation子类一样。

于 2011-01-18T14:53:41.507 回答