开发者

Clang warning: Value stored to 'pool' during its initialization is never read

- (void)main {
    NSA开发者_JAVA技巧utoreleasePool *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 see why this code get such a warning, especially while it has almost exactly same structure as the main function in main.m generated by Xcode itself which does not get 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;
}


I believe the issue is the while (YES) statement. Clang is seeing that as an endless loop that you'll never get outside of, so that the code below that block will never be reached.

Just changing it to the following (a BOOL variable set to YES outside of 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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜