运行时错误:__NSAutoreleaseNoPool():...自动释放,没有池 - 只是泄漏

发布于 2024-12-01 15:01:20 字数 1534 浏览 0 评论 0原文

当我编译 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 是正确的解决方案吗?

I am getting a list of errors like the following error when I compile my project for 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 

It appears because of the following 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];
}

When I add and NSAutoreleasePool to my function

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

The error is gone but I don't get any output from my function. Is adding NSAutoreleasePool the right solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

污味仙女 2024-12-08 15:01:20

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

池 一种生成方式在后台执行的事情是通过调用 NSObject 的 PerformSelectorInBackground 方法,我假设您正在这样做

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

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

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

}

能澄清这一点

希望丹尼尔

You get the message on the console because you are running the start method on a background thread and have not placed an Autorelease pool in place that will take care of reclaiming objects once they have been released (release count == 0 ),this doesnt happen in the main thread because the main thread already has a pool in place, for background threads u spawn you are responsible to set up the autorelease pool... your solution is the right solution to the problem.. so here is an example of when to and where to use autorelease pool

One way to spawn something to execute in the background is by calling performSelectorInBackground method of NSObject which i assume you are doing

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

Now this method is going to execute on a background thread and you need to place an Autorelease pool in place in order for it not to leak, like so

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

}

hope that clears it up

Daniel

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文