c++ - 映射类导致内存泄漏

标签 c++ maps

 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/

相关文章:

c++ - 如何调整 CDockablePane 的大小?

c++ - 在 Ubuntu 12.04 上链接 - 对 `clock_gettime@GLIBC_2.17' 的 undefined reference

c++游戏引擎设计和超出范围的对象

javascript - 在圆形雷达数学方法中表示点

ios - GMSCameraPosition 不准确

c++ - 在没有管理员权限的情况下以 C++ 编程方式将程序添加到启动

c++ - 如何使 QMdiArea 子窗口小部件不可调整大小?

java - 使用java google map api生成给定地址的3d坐标

python - 在 map 中绘制路线

android - 在android中的谷歌地图上画点