如果在lambda中捕获了独特的指针的元组,则潜在的内存泄漏

发布于 2025-02-10 05:13:21 字数 1631 浏览 2 评论 0原文

clang-tidy扫描构建警告此代码中的潜在内存泄漏:

#include <tuple>
#include <memory>

int main()
{
    auto lambda = [tuple = std::make_tuple(std::make_unique<int>(42))] {};
}
$ clang-tidy main.cpp -checks="clang*"
1 warning generated.
/foo/main.cpp:7:1: warning: Potential leak of memory pointed to by field '_M_head_impl' [clang-analyzer-cplusplus.NewDeleteLeaks]
}
^
/foo/main.cpp:6:44: note: Calling 'make_unique<int, int>'
    auto lambda = [tuple = std::make_tuple(std::make_unique<int>(42))] {};
                                           ^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-redhat-linux/11/../../../../include/c++/11/bits/unique_ptr.h:962:30: note: Memory is allocated
    { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/foo/main.cpp:6:44: note: Returned allocated memory
    auto lambda = [tuple = std::make_tuple(std::make_unique<int>(42))] {};
                                           ^~~~~~~~~~~~~~~~~~~~~~~~~
/foo/main.cpp:7:1: note: Potential leak of memory pointed to by field '_M_head_impl'
}
^

我看不到任何东西,还是是误报?


环境:Clang 13.0.0,GCC 11.3.1。

编译命令:

[
{
  "directory": "/foo",
  "command": "/usr/bin/g++ -std=c++17 /foo/main.cpp",
  "file": "/foo/main.cpp"
}
]

注意:问题可与-STD = C ++ 17-STD = C ++ 20,但不使用-STD = C ++ 14

clang-tidy and scan-build warn about a potential memory leak in this code:

#include <tuple>
#include <memory>

int main()
{
    auto lambda = [tuple = std::make_tuple(std::make_unique<int>(42))] {};
}
$ clang-tidy main.cpp -checks="clang*"
1 warning generated.
/foo/main.cpp:7:1: warning: Potential leak of memory pointed to by field '_M_head_impl' [clang-analyzer-cplusplus.NewDeleteLeaks]
}
^
/foo/main.cpp:6:44: note: Calling 'make_unique<int, int>'
    auto lambda = [tuple = std::make_tuple(std::make_unique<int>(42))] {};
                                           ^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-redhat-linux/11/../../../../include/c++/11/bits/unique_ptr.h:962:30: note: Memory is allocated
    { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/foo/main.cpp:6:44: note: Returned allocated memory
    auto lambda = [tuple = std::make_tuple(std::make_unique<int>(42))] {};
                                           ^~~~~~~~~~~~~~~~~~~~~~~~~
/foo/main.cpp:7:1: note: Potential leak of memory pointed to by field '_M_head_impl'
}
^

Is there anything I do not see, or is it a false positive?


Environment: clang 13.0.0, gcc 11.3.1.

Compile commands:

[
{
  "directory": "/foo",
  "command": "/usr/bin/g++ -std=c++17 /foo/main.cpp",
  "file": "/foo/main.cpp"
}
]

Notes: the issue is reproducible with -std=c++17 and -std=c++20, but not with -std=c++14.

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

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

发布评论

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

评论(2

佼人 2025-02-17 05:13:21

lambda在范围耗尽后被摧毁,并被捕获 - 随着元组的捕获,它也被捕获,它也会被破坏,并与之一起弹性,因此智能指针将其存储在那里。

尝试查看:

#include <iostream>
#include <memory>
#include <tuple>

struct S
{
    S() { std::cout << "constructed" << std::endl; }
    ~S() { std::cout << "destructed" << std::endl; }
};

int main()
{
    auto lambda = [tuple = std::make_tuple(std::make_unique<S>())] {};
    return 0;
}

请参阅 godbolt

显然是一个假阳性...

The lambda gets destructed after running out of scope, together with it is capture – and as the tuple is captured by value it gets destructed as well, together with it the smart pointer and thus the object stored there.

Try this to see:

#include <iostream>
#include <memory>
#include <tuple>

struct S
{
    S() { std::cout << "constructed" << std::endl; }
    ~S() { std::cout << "destructed" << std::endl; }
};

int main()
{
    auto lambda = [tuple = std::make_tuple(std::make_unique<S>())] {};
    return 0;
}

See on godbolt.

So apparently a false positive...

给妤﹃绝世温柔 2025-02-17 05:13:21

clang-tidy中的一个未解决的错误:
https://github.com/llvm/llvm/llvm/llvm/llvm-project/issues/552219

它与此注释

An unsolved bug in clang-tidy:
https://github.com/llvm/llvm-project/issues/55219

It doesn't have anything to do with tuples or smart pointers as seen in the simplified reproduction in this comment.

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