How to set up an autorelease pool when using [NSThread detachNewThreadSelector:toTarget:withObject:]

前端 未结 5 387
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 17:35

Hi I\'m usuing [NSThread detachNewThreadSelector:toTarget:withObject:] and I\'m getting a lot of memory leaks because I have no autorelease pool set up for the detached thr

5条回答
  •  鱼传尺愫
    2021-01-14 18:22

    Do it in the method you call. Essentially, you should set up the method that gets called as a self-contained work unit (in fact, it will then be compatible with being called through either [NSOperation][1] or Grand Central Dispatch, too: both better ways of organising concurrent work).

    But what if I can't change the implementation of the method I'm calling on a new thread?

    In that case, you would go from doing this:

    [NSThread detachNewThreadSelector: @selector(blah:) toTarget: obj withObject: arg]
    

    to doing this:

    [NSThread detachNewThreadSelector: @selector(invokeBlah:) toTarget: self withObject: dict]
    
    - (void)invokeBlah: (id)dict {
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      id obj = [dict objectForKey: @"target"];
      id arg = [dict objectForKey: @"argument"];
      [obj blah: arg];
      [pool release];
    }
    

    rather than using the dictionary, you could also create an NSInvocation that encapsulates the remote object call. I just chose a dictionary because it's the quickest way to show the situation in a SO answer. Either would work.

提交回复
热议问题