Runtime error: __NSAutoreleaseNoPool(): …autoreleased with no pool in place - just leaking

守給你的承諾、 提交于 2019-12-13 04:21:37

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/7189313/runtime-error-nsautoreleasenopool-autoreleased-with-no-pool-in-place

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!