开发者

NSThread and memory management

开发者 https://www.devze.com 2022-12-28 18:01 出处:网络
Imagine I create and execute an NSThread object using detachNewThreadSelector:toTarget:withObject:. The method executed by the thread might look like this:

Imagine I create and execute an NSThread object using detachNewThreadSelector:toTarget:withObject:. The method executed by the thread might look like this:

- (void)search {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // perform a lengthy search here.

    [pool release];
}

I then might use the ca开发者_开发问答ncel method to kill the thread while it's running, before the pool gets released. What happens to the NSAutoreleasePool object? I suppose it will leak, won't it?


-(void)cancel doesn't force cancellation, it merely requests it. Your -(void)search will run to completion. Read "Responding to the Cancel Command" for details.


I highly recommend NSOperationQueue for simple concurrent tasks.

0

精彩评论

暂无评论...
验证码 换一张
取 消