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 があるように見えます。このオブジェクトは、操作が完了したときに、自動解放されたすべてのオブジェクトに対して release を呼び出すと想定しても安全ですか?

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 に答える