ios - 运行时错误 : __NSAutoreleaseNoPool(): . ..autoreleased 没有适当的池 - 只是泄漏

标签 ios multithreading memory-leaks xcode4

当我为 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/

相关文章:

ios - 通过 navigationController 使用 segue 传递数据

ios - ScrollView 中的全屏图像

java - 仅使用本地锁从 BST 进行多线程删除

c++ - 使用 mutex 和 condition_variable 时的异常

c - Valgrind 内存泄漏这是什么错误?

ios - 有人可以解释一下这个 Objective-C 语法吗?

ios - 使用后台线程更新界面的更快方法

c# - SQL Server Compact Edition 4 - AccessViolationException

c++ - STL map and list using in c++的一些问题

iphone - 我的 NSDateFormatter 脚本中存在重大泄漏