views:

130

answers:

2
+2  Q: 

Weird memory leak

Hi

I am having a memory leak problem and it is actually generating from the following structure array:

TagStruct TagData [] =
{

    { Tag_SecurityToken, string("x-abc-security-token"), string("ab-security-token") } ,
    { Tag_XYZ, string("x-abc-xyz"), string("ab-xyz") },
    { Tag_ChunkCount, string("x-abc-meta-chunk"), string("ab-meta-chunk") },
    { Tag_OriginalFileSize, string("x-abc-meta-totalsize"), string("ab-meta-totalsize")}

};

the weird thing is of course the memory leak issue and the weirdest issue is that when I close the client *VS dumps the first, third and fourth elements but not the 2nd element (Tag_XYZ).* The memory leak seems to generate on the first run before I even call the array, and bare in mind that I only use the array for checking purposes:

string Get_Tag (Tags tag)
{
    return  m_ClientType == CType_ABC ? TagData[tag].strABC : TagData[tag].strAB;
}

Thats actually the whole reason for it. So there is no inline heap/memory allocation of any kind.

also the Tag structure looks sth like this:

struct TagStruct
    {
     Tags tag;
     std::string strABC;
     std::string strAB;
    };

I would also like to let you know that this is a global array and I have also tried to make it static but kept getting Memory leak, also I've tried declaring the strings in the array as "str" rather than string("str") but kept getting memory leaks. Yes the memory leak is static, meaning that it does not build over time but still its better to keep a clean code. Any suggestions??

+7  A: 

That's an example of a false leak - the leak detector sees that the allocated memory is not freed at the point the detector is run. But the detector is run prior to the static objects destructors and therefore prior to the memory being freed. That's why it's a leak from the detector point of view but it is not really a leak.

sharptooth
was thinking that my self but I wanted to check...
Red Serpent
I would also like to know why VS only dumps the 1st, 3rd and 4th elements of the array and not the 2nd???
Red Serpent
@Red Serpent So as a workaround, you could make the struct non-static by putting it into the Get_Tag function. I don't know what the compiler can do to optimize that but my guess is that there will be no huge performance hit.
mxp
+1  A: 

it is a false alarm, but even if it wasn't it would have meant nothing, being a global (or static) variable the space for this array is calculated and allocated at compile time, to be stored in the data segment, this memory never gets released. memory leaks occurs when memory gets dynamically allocated in run time without being released

Alon