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

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

    Create the new thread:

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

    Create the method that is called by the new thread.

    
    - (void)myMethod
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        // Your Code
        [pool release];
    } 

    What if you need to do something to the main thread from inside your new thread (for example, show a loading symbol)? Use performSelectorOnMainThread.

    [self performSelectorOnMainThread:@selector(myMethod) withObject:nil waitUntilDone:false];
    

    Refer :- iPhone SDK Examples

提交回复
热议问题