可以std ::退出泄漏内存
Can std::exit leak memory?
要回答我的问题,我进行了一些测试:
(重复以下是必要的)
#include <cstdlib>
#include <memory>
我制作了一个称为std::exit
的函数,在main
函数中使用std::unique_ptr
。
void some_function()
{
std::exit(EXIT_SUCCESS);
}
int main()
{
std::unique_ptr<int> relying_on_raii{new int{5}};
}
如果我在unique_ptr
声明后打电话给some_function
,则内存可能会泄漏。
我从内存博士那里获得的两个日志在以下行中有所不同:
1 potential leak(s) (suspected false positives)
[…]
6 unique, 6 total, 797 byte(s) of still-reachable allocation(s)
vs。
1 potential leak(s) (suspected false positives)
[…]
7 unique, 7 total, 801 byte(s) of still-reachable allocation(s)
可以看出,在第二个示例中,发生了第7个电势泄漏,即大小为4个字节,恰好是int
的大小。当我用double重复此测试时,实际上它是std::exit
的测试中的805 byte(s)
。
std::exit
是使用安全功能,还是应该始终从主返回以防止内存泄漏?
STD ::退出安全功能,还是应该始终从主返回以防止内存泄漏?
是的,它可能会泄漏内存。
那是不太令人担忧的问题。更重要的问题是,如果您的程序获得无法通过关闭流程而无法释放的资源。为了处理这种情况,最好要么以某种错误状态返回,直到您能够从main
退出或使用try-throw-catch
来确保main
能够捕获所有未知的例外并优雅地退出。
相关文章:
- 从构造函数抛出异常时如何克服内存泄漏
- malloc() 可能出现内存泄漏
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 尽管遵循了规则,内存泄漏在哪里
- 为什么调用堆栈数组会导致内存泄漏
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 使用模板类的自定义列表类型中的内存泄漏
- 为什么以下C++代码中存在内存泄漏?
- OpenCV 我应该使用智能指针来防止内存泄漏吗?
- 我是否生成线程并导致内存泄漏?
- 多线程程序中出现意外的内存泄漏
- 为什么此函数会导致内存泄漏?
- 在 C++ 库中使用cythonized python时内存泄漏
- 需要帮助查找内存泄漏
- 瓦尔格林德的内存泄漏使用新的
- 无法找出我的代码中的内存泄漏
- C++ 结构内部的unordered_map会导致内存泄漏问题吗?
- 可视化 使用 VS Code 查找C++应用程序中的内存泄漏
- Shared_ptr双链接列表内存泄漏
- C++ 在类中使用常量引用文本时 O2 内存泄漏