[英]Memory leak when declaring string
I'm working on a C++ console application that is required to have 0 memory leaks.我正在开发一个需要 0 内存泄漏的 C++ 控制台应用程序。 However, I'm very new to memory leaks and I wonder why this is already causing a memory leak, while there is barely any code:
但是,我对内存泄漏很陌生,我想知道为什么这已经导致内存泄漏,而几乎没有任何代码:
int main()
{
std::string httpResult;
_CrtDumpMemoryLeaks();
return 0;
}
This is the given memory leak in VS 2019:这是 VS 2019 中给定的内存泄漏:
Detected memory leaks!
Dumping objects ->
{93} normal block at 0x00000146FEB63BD0, 16 bytes long.
Data: <x > 78 FB 15 D6 13 00 00 00 00 00 00 00 00 00 00 00
Object dump complete.
The only thing I'm doing is declaring a std::string.我唯一要做的就是声明一个 std::string。 Why is that a memory leak?
为什么会出现内存泄漏?
Consider this:考虑一下:
int main()
{
std::string httpResult;
_CrtDumpMemoryLeaks();
std::abort();
return 0;
}
This code has a memory leak, because on calling abort
destructors of objects with automatic storage duration are not called.这段代码存在内存泄漏,因为在调用具有自动存储持续时间的对象的
abort
析构函数时不会被调用。 _CrtDumpMemoryLeaks();
cannot look ahead to know if resources will be cleaned up after it is called.无法提前知道资源在调用后是否会被清理。 There is no memory leak in this code
这段代码没有内存泄漏
int main()
{
{
std::string httpResult;
}
_CrtDumpMemoryLeaks();
return 0;
}
(and _CrtDumpMemoryLeaks();
does not see any candidates for a memory leak.) (和
_CrtDumpMemoryLeaks();
没有看到任何内存泄漏的候选者。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.