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

前端 未结 5 407
爱一瞬间的悲伤
爱一瞬间的悲伤 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:24

    in the method you call with the thread... i.e. given this...

    [NSThread detachNewThreadSelector:@selector(doStuff) toTarget:self withObject:nil];
    

    Your method would be...

    - (void)doStuff {
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      //Do stuff
      [pool release];
    }
    

提交回复
热议问题