【问题标题】:Runtime error: __NSAutoreleaseNoPool(): ...autoreleased with no pool in place - just leaking运行时错误:__NSAutoreleaseNoPool(): ...autoreleased,没有适当的池 - 只是泄漏
【发布时间】:2011-11-03 14:18:24
【问题描述】:

我在为 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 是正确的解决方案吗?

【问题讨论】:

    标签: ios multithreading memory-leaks xcode4


    【解决方案1】:

    您在控制台上收到消息,因为您在后台线程上运行 start 方法,并且没有放置一个自动释放池,该池将负责在释放对象后回收对象(释放计数 == 0),这不会发生在主线程中,因为主线程已经有一个池,对于你产生的后台线程,你负责设置自动释放池......你的解决方案是问题的正确解决方案......所以这里有一个何时何地使用自动释放池的示例

    在后台生成要执行的东西的一种方法是调用 NSObject 的 performSelectorInBackground 方法,我假设你正在这样做

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

    现在此方法将在后台线程上执行,您需要放置一个自动释放池以使其不会泄漏,就像这样

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

    希望可以解决

    丹尼尔

    【讨论】:

    • 好的,谢谢,但您能否更具体地告诉我应该在哪里放置自动释放池?我的意思是我们通常把它们放在哪里。对不起,如果答案很明显,但我是 iOS 开发的新手。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多