我在 iphone 上运行一些小代码时遇到了麻烦。
基本上,我只需按下一个按钮,它就会调用,它在后台线程上runTest
运行方法。test
这就是我创建自动释放池的原因。
如果我运行下面的代码,我会在控制台上收到一条漂亮的消息:
2010-09-07 11:45:15.527 test[1312:207] *** -[CFString release]: message sent to deallocated instance 0x3d52ba0
-(void) test {
NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];
NSString *xml = [[NSString alloc] initWithFormat:@"<%@>", @"msg"];
NSLog(@"%@\n",xml);
[xml release];
[apool release]; // <-- this line throws the error msg
}
- (IBAction) runTest: (id)sender
{
[self performSelectorInBackground:@selector(test) withObject:nil];
}
我发现:如果我不在test
后台线程(没有自动释放池)上运行,只需调用[self test]
,代码就可以正常工作。
所以,我认为问题出在线程+自动释放池周围,我做错了什么?我该如何解决?
PS我NSZombie
启用了标志。