Haitham Khedr
Haitham Khedr

Reputation: 99

map class causes memory leaks

 int main()
    {
    map<string,string> x;
    x["Haitham"]="[email protected]";
    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.

Upvotes: 0

Views: 97

Answers (1)

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

This issue is that the you're calling the function that checks for memory leaks too soon. The map hasn't been destroyed at the point you call _CrtDumpMemoryLeaks.

Change your code to this:

int main()
{
    {
        map<string,string> x;
        x["Haitham"]="[email protected]";
        x.clear();
    }
    _CrtDumpMemoryLeaks();
    return 0;
}

This should now show that the map is destrouyed, since it is local to the { } block.

Upvotes: 2

Related Questions