Clan warning: a value stored in the "pool" during its initialization is never read

- (void)main {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Warning goes here

    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    while (YES) {
        NSAutoreleasePool *subPool = [[NSAutoreleasePool alloc] init];
        [runLoop run];
        [subPool drain];
    }

    [pool drain];
}

I don’t understand why this code receives such a warning, especially if it has almost the same structure as the main function main.m generated by Xcode itself, which does not receive the same warning:

int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}
+5
source share
1 answer

I believe the problem is the operator while (YES). Clang sees this as an endless loop that you will never get outside, so the code below this block will never be reached.

Just changing it to the following (a variable BOOLset to YESoutside the block) will remove the warning:

int main (int argc, const char * argv[])
{

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

    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];

    BOOL keepRunning = YES;

    while (keepRunning) {
        NSAutoreleasePool *subPool = [[NSAutoreleasePool alloc] init];
        [runLoop run];
        [subPool drain];
    }

    [pool drain];

    return 0;
}
+5

All Articles