运行时错误:__NSAutoreleaseNoPool():…自动释放没有到位的池 – 只是泄漏

当我编译我的iOS项目时,出现如下错误的错误列表。

2011-08-25 12:32:44.016 rtsp[55457:6003] *** __NSAutoreleaseNoPool(): Object 0x64095a0 of class __NSArrayM autoreleased with no pool in place - just leaking 

看起来是因为以下function

 - (void) start { //Existing code session = [[RTSPClientSession alloc] initWithURL: [NSURL URLWithString: @"rtsp://video3.americafree.tv/AFTVComedyH2641000.sdp"]]; [session setup]; NSLog(@"getSDP: --> %@",[ session getSDP ]); NSArray *array = [session getSubsessions]; for (int i=0; i < [array count]; i++) { RTSPSubsession *subsession = [array objectAtIndex:i]; [session setupSubsession:subsession clientPortNum:0 ]; subsession.delegate=self; [subsession increaseReceiveBufferTo:2000000]; NSLog(@"%@", [subsession getProtocolName]); NSLog(@"%@", [subsession getCodecName]); NSLog(@"%@", [subsession getMediumName]); NSLog(@"%d", [subsession getSDP_VideoHeight]); NSLog(@"%d", [subsession getServerPortNum]); } [session play]; NSLog(@"error: --> %@",[session getLastErrorString]); [session runEventLoop:rawsdp]; } 

当我添加和NSAutoreleasePool到我的function

 - (void) start { NSAutoReleasePool *pool=[[NSAutoReleasePool alloc] init]; session = [[RTSPClientSession alloc] initWithURL:[NSURL ... ... [pool drain]; } 

错误消失了,但是我没有从我的函数得到任何输出。 是添加NSAutoreleasePool正确的解决scheme?

你在控制台上得到消息,因为你在后台线程上运行启动方法,并没有放置一个自动释放池,一旦它们被释放(释放计数== 0)将照顾回收对象,这不会发生在主线程,因为主线程已经有一个池,为后台线程产卵你负责设置autorelease池…你的解决scheme是正确的解决scheme的问题..所以这里是一个例子,当到哪里使用autorelease池

一种方法来产生在后台执行的东西是通过调用NSObject的performSelectorInBackground方法,我假设你正在做

 [myObject performSelectorInBackground:(@selector(myBackgroundMethod:) withObject:nil]; 

现在这个方法将在后台线程上执行,并且你需要放置一个Autorelease池以便它不泄漏,就像这样

  -(void)myBackgroundMethod:(id)sender { NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init]; //do stuff [pool release]; } 

希望清除它

丹尼尔