在NSOperation中自动释放对象是否安全?

时间:2011-01-18 13:56:01

标签: cocoa nsoperation autorelease

我正在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 - 当操作完成时,假设这个对象会调用我所有自动释放的对象上的释放是否安全?

1 个答案:

答案 0 :(得分: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

正如我们所看到的,有NSInvocationNSSet泄漏的情况,但-performSomeTask:中的自动释放字符串未泄漏,因此为该调用操作创建了自动释放池。

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