多线程和“泄漏”警告

时间:2010-01-01 17:02:44

标签: iphone objective-c cocoa-touch multithreading

我正在创建一个这样的新线程:

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

在我做的方法中:

- (void) myMethod:(NSString*)path{
NSAutoreleasePool *pool = [NSAutoreleasePool alloc];
[UIImagePNGRepresentation(theImage) writeToFile:[path stringByAppendingString:@".png"] atomically:NO];
[pool release];
}

但不断收到这些警告:

*** _NSAutoreleaseNoPool(): Object 0x194b1c0 of class NSConcreteMutableData autoreleased with no pool in place - just leaking
Stack: (0x305a2e6f 0x30504682 0x309084ff 0x3a0d 0x3050a79d 0x3050a338 0x9100cfbd 0x9100ce42)

*** _NSAutoreleaseNoPool(): Object 0x19014c0 of class NSCFString autoreleased with no pool in place - just leaking
Stack: (0x305a2e6f 0x30504682 0x3a30 0x3050a79d 0x3050a338 0x9100cfbd 0x9100ce42)

是不是在被调用的方法中创建了一个NSAutoreleaseNoPool对象,然后以正确的方式释放它以使用线程化的@selector?

2 个答案:

答案 0 :(得分:6)

您没有完全初始化自动释放池:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

答案 1 :(得分:0)

试试这段代码:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

// do stuff

[pool drain];

希望这有帮助