【问题标题】:Clang warning: Value stored to 'pool' during its initialization is never readClang 警告:在初始化期间存储到“池”的值永远不会被读取
【发布时间】:2011-09-11 08:23:18
【问题描述】:
- (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];
}

我不明白为什么这段代码会收到这样的警告,尤其是当它与 main.m 中由 Xcode 本身生成的 main 函数几乎完全相同的结构时,它没有收到相同的警告:

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

【问题讨论】:

    标签: objective-c ios clang


    【解决方案1】:

    我认为问题在于while (YES) 声明。 Clang 将其视为您永远无法摆脱的无限循环,因此永远无法到达该块下方的代码。

    只需将其更改为以下内容(在块外将BOOL 变量设置为YES)即可消除警告:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      • 2012-01-19
      相关资源
      最近更新 更多