Clang 警告:值存储到“池”中在初始化期间永远不会被读取

发布于 2024-11-14 17:40:15 字数 648 浏览 2 评论 0原文

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

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

int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}
- (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 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情感失落者 2024-11-21 17:40:15

我认为问题在于 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文