读书人

这是咋回事?内存就这样写漏了

发布时间: 2012-11-03 10:57:43 作者: rapoo

这是怎么回事?内存就这样写漏了?

C/C++ code
#include <crtdbg.h>#include <vector>int main(int argc, char **argv)    {    std::vector<std::string> svec;    svec.push_back("Hello");    _CrtDumpMemoryLeaks();    return 0;    }


Detected memory leaks!
Dumping objects ->
{131} normal block at 0x002E4778, 8 bytes long.
Data: < G. > 18 47 2E 00 00 00 00 00
{130} normal block at 0x002E4718, 32 bytes long.
Data: <xG. Hello > 78 47 2E 00 48 65 6C 6C 6F 00 CD CD CD CD CD CD
{128} normal block at 0x002E4688, 8 bytes long.
Data: <( " > 28 FC 22 00 00 00 00 00
Object dump complete.

朋友们帮我看看。

[解决办法]
这样写会泄漏,为什么呢?
C/C++ code
#include <iostream>#include <crtdbg.h>int main(int argc, char **argv){    _CrtDumpMemoryLeaks();    return 0;}
[解决办法]
可以看一下STL的内存管理部分
可能是因为svec占用的内存要到return 0时才会释放
所以执行到_CrtDumpMemoryLeaks时,它把现在所有没有释放的内存当做memory leak
STL肯定不会弱到,定义一个vector就会内存泄露

读书人网 >C++

热点推荐