void String::operator+=( String &pData)
{
int nLen = 0; // for store length of string
// check string is null or not
if(pData.m_pText != NULL)
{
// length of string
nLen = (int) strlen(pData.m_pText);
}
// Total string length
int nTotLen = m_nLength + nLen;
//object of string class
String Stemp;
//allocate memory
Stemp.m_pText = new char[nTotLen + 1];
// copy data
strcpy(Stemp.m_pText, m_pText);
// append string
strcat(Stemp.m_pText, pData.m_pText);
//allocate memory
m_pText = new char[nTotLen + 1];
// copy string
strcpy(m_pText, Stemp.m_pText);
// set new length
m_nLength = nTotLen;
}
when i run this code heap memory leak found. why this memory leak come?
what i want to do avoid memory leak?