Thread: Huge memory leak

  1. #1
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215

    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?
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    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.
    Last edited by Ancient Dragon; 10-30-2005 at 02:34 PM.

  3. #3
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    yea, I fixed that heh. Still causes a leak tho so I'm switching over to MFC and hopefully CString will return properly
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detecting memory leak in complicating source code
    By mosxopul in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2009, 11:41 AM
  2. Memory leak in this case?
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2008, 05:05 AM
  3. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 06:50 AM
  4. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM