如果在lambda中捕获了独特的指针的元组,则潜在的内存泄漏
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
lambda在范围耗尽后被摧毁,并被捕获 - 随着元组的捕获,它也被捕获,它也会被破坏,并与之一起弹性,因此智能指针将其存储在那里。
尝试查看:
请参阅 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:
See on godbolt.
So apparently a false positive...
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.