当我为 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
它的出现是因为下面的函数
- (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
添加到我的函数时
- (void) start {
NSAutoReleasePool *pool=[[NSAutoReleasePool alloc] init];
session = [[RTSPClientSession alloc] initWithURL:[NSURL ...
...
[pool drain];
}
错误消失了,但我的函数没有得到任何输出。添加 NSAutoreleasePool
是正确的解决方案吗?
最佳答案
您在控制台上收到消息是因为您在后台线程上运行 start 方法并且没有放置一个自动释放池来处理对象被释放后的回收(释放计数 == 0),这不会发生在主线程中,因为主线程已经有一个池,对于你产生的后台线程,你负责设置自动释放池......你的解决方案是解决问题的正确方法......所以这是一个何时何地使用自动释放池的示例
产生一些东西在后台执行的一种方法是调用 NSObject 的 performSelectorInBackground 方法,我假设你正在做
[myObject performSelectorInBackground:(@selector(myBackgroundMethod:) withObject:nil];
现在这个方法将在后台线程上执行,你需要放置一个自动释放池以防止它泄漏,就像这样
-(void)myBackgroundMethod:(id)sender
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
//do stuff
[pool release];
}
希望一切顺利
丹尼尔
关于ios - 运行时错误 : __NSAutoreleaseNoPool(): . ..autoreleased 没有适当的池 - 只是泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7189313/