-
Huge memory leak
I'm getting a large memory leak in my code, its even causing my program to shutdown after a little bit and I'm having trouble finding what is wrong. I ran rational purifyplus on it and it gave me the distinct location but I don't see the problem. Here's some code:
Calling Code:
Code:
std::string str = "";
str += ZPacketData->GetData()->back()->GetMACAddress(TRUE);
GetMACAddress: (where the memory leak supposedly happens)
Code:
std::string ZeroPacket::GetMACAddress(BOOL IsSource)
{
std::string strReturn = "";
char cBuff[20];
char* pChar;
if( IsSource )
pChar = (char*)(m_Data);
else
pChar = (char*)(m_Data+6);
for( int i=0; i<6; i++ )
{
sprintf(cBuff, ("02x:"), *(BYTE*)pChar++);
strReturn += cBuff;
}
return strReturn;
}
The code is pretty straightforward and I'm assuming it has something to do with std::string. Also, PurifyPlus is stating that the leak is on the heap, but I didn't allocated anything to the heap except for m_Data. Any Ideas?
-
Code:
sprintf(cBuff, ("02x:"), *(BYTE*)pChar++);
The above line is wrong -- where is the '%'?
Code:
sprintf(cBuff, ("%02x:"), (unsigned)*(BYTE*)pChar++);
Not sure if it will help or not, but you might try typcasting to unsigned int.
-
yea, I fixed that heh. Still causes a leak tho so I'm switching over to MFC and hopefully CString will return properly
-
It is highly unlikely that the problem is in the string implementation, since so many people use it and you haven't found anybody else with this problem. It is either a problem with something else, or a problem with how you are using the string.