为了回答我的问题,我进行了一些测试:
#include <cstdlib>
#include <memory>
我做了一个函数,调用
std::exit
,带有
std::unique_ptr
main
作用
void some_function()
{
std::exit(EXIT_SUCCESS);
}
int main()
{
std::unique_ptr<int> relying_on_raii{new int{5}};
}
如果我打电话
some_function
之后
unique_ptr
声明时,内存可能会泄漏。
Dr. Memory
在以下行中有所不同:
1 potential leak(s) (suspected false positives)
[â¦]
6 unique, 6 total, 797 byte(s) of still-reachable allocation(s)
1 potential leak(s) (suspected false positives)
[â¦]
7 unique, 7 total, 801 byte(s) of still-reachable allocation(s)
可以看出,在第二个示例中,发生了第7次潜在泄漏,大小为4字节,正好是
int
805 byte(s)
在测试中
标准::退出
.
标准::退出
使用安全的函数,还是应该始终从main返回以防止内存泄漏?