IOS is auto-implemented without a pool in place - but I'm creating ARP!

So, I use [NSThread detachNewThreadSelector] to create a new thread, and I get errors "autoreleased with no pool in place" in the console. I know that this can happen if you were unable to create an auto-release pool, but the fact is that I create it. I use similar code in other parts of the same application and DO NOT get these errors.

Here is the relevant code:

- (void) startThread:(NSString*)strURL
{
    // start new thread to load image
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [NSThread detachNewThreadSelector:@selector(loadImageFromURL:) toTarget:self withObject:strURL];
    [pool release];
}

- (void) loadImageFromURL:(NSString*)strURL
{
    NSNumber* nn = [NSNumber numberWithInt:self.tag];
    NSLog(@"loadURL: Tag number == %i", [nn intValue]);

   // other code here actually does the work
}

loadImageFromURL , ( ) - , ( , !). , - ​​ NSNumber.

, :

__NSAutoreleaseNoPool(): Object 0x535c0e0 of class NSCFNumber autoreleased with no pool in place - just leaking

, AR, .

, !

!

+3
3

, . , :

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

loadImageFromURL:

[pool drain];

.

, , startThread:. , " ".

+5

- (void) startThread:(NSString*)strURL, - (void) loadImageFromURL:(NSString*)strURL , .

NSAutoreleasePool, , startThread:, , . NSAutoreleasePool, .

:

- (void) startThread:(NSString*)strURL
{
    // start new thread to load image
    [NSThread detachNewThreadSelector:@selector(loadImageFromURL:) toTarget:self withObject:strURL];
}

- (void) loadImageFromURL:(NSString*)strURL
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSNumber* nn = [NSNumber numberWithInt:self.tag];
    NSLog(@"loadURL: Tag number == %i", [nn intValue]);

   // other code here actually does the work

    [pool drain];
}

, @Carl Norum, drain release, autorelelase.

+4

A solution for a similar task, but using ARC.

If you use ARC, you may receive the error message "NSAutoreleasePool" Unavailable: Unavailable in automatic link counting mode. "

Using:

- (void) startThread:(NSString*)strURL
{
    // start new thread to load image
    [NSThread detachNewThreadSelector:@selector(loadImageFromURL:) toTarget:self withObject:strURL];
}

- (void) loadImageFromURL:(NSString*)strURL
{
    @autoreleasepool {          
        NSNumber* nn = [NSNumber numberWithInt:self.tag];
        NSLog(@"loadURL: Tag number == %i", [nn intValue]);

        // other code here actually does the work   
    }
}
+1
source

Source: https://habr.com/ru/post/1787514/


All Articles