int main()
{
map<string,string> x;
x["Haitham"]="000@hotmail.com";
x.clear();
_CrtDumpMemoryLeaks();
return 0;
}
when i test for memory leaks for this program the output is Detected memory leaks!
{152} normal block at 0x0070ABD8, 8 bytes long.
Data: <4 p > 34 AB 70 00 00 00 00 00
{151} normal block at 0x0070AB90, 8 bytes long.
{150} normal block at 0x0070AB08, 72 bytes long.
Data: <
p
p ` p > 60 A9 70 00 60 A9 70 00 60 A9 70 00 01 00 CD CD{145} normal block at 0x00704C40, 8 bytes long.
Data: < ^ > E4 FE 5E 00 00 00 00 00
{144} normal block at 0x0070A960, 72 bytes long.
Data: < p p p > 08 AB 70 00 08 AB 70 00 08 AB 70 00 01 01 CD CD
Data: < p > 18 AB 70 00 00 00 00 00
Object dump complete.
最佳答案
这个问题是因为您过早地调用了检查内存泄漏的函数。在您调用 _CrtDumpMemoryLeaks
时,map
尚未被销毁。
将您的代码更改为:
int main()
{
{
map<string,string> x;
x["Haitham"]="000@hotmail.com";
x.clear();
}
_CrtDumpMemoryLeaks();
return 0;
}
这应该表明 map
已被销毁,因为它位于 { }
block 的本地。
关于c++ - 映射类导致内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26980394/