Memory Leak Problem
-
Hi Developers, I was facing "Memory Leak" problem, from this chunk of code. I have allocate a memory for a structure( pRecStruct ) & after that it has stored in a map( CMapWordToPtr - pMapUserBoard ) for furher use. I have tried to delete as well, but the next time if i am trying to use. Plese tell me the issue. if( bSymAlreadyPresent == false ) { CString str(pDBStruct->Symp); strTemp.Format( _T("%d"), userClipIndx + 1 ); str += " Symptom Dropped on Clipboard " + strTemp; MessageBox( NULL, str, "", MB_OK ); NEWDB_STRUCT near *pRecStruct=NULL; pRecStruct=new NEWDB_STRUCT; int cLen=strlen(pDBStruct->Symp); pRecStruct->Symp=new char[cLen+1]; memset(pRecStruct->Symp, NULL,cLen+1); memcpy(pRecStruct->Symp,pDBStruct->Symp,cLen); pRecStruct->Remedy=new char[strlen(pDBStruct->Remedy)+1]; memset(pRecStruct->Remedy, NULL,strlen(pDBStruct->Remedy)+1); memcpy(pRecStruct->Remedy,pDBStruct->Remedy,strlen(pDBStruct->Remedy)); pRecStruct->RecSize=pDBStruct->RecSize; pRecStruct->LevelNum=pDBStruct->LevelNum; pRecStruct->RemedyCnt=pDBStruct->RemedyCnt; iTemp = pMapUsrClipboard->GetCount(); pRecStruct->SymNo = iTemp; pRecStruct->Author=pDBStruct->Author; pRecStruct->RecNo=pDBStruct->RecNo; pRecStruct->ParOffset=pDBStruct->ParOffset; pRecStruct->NextOffset=pDBStruct->NextOffset; pRecStruct->CurOffset=pDBStruct->CurOffset; pRecStruct->SymType=0; pMapUsrClipboard->SetAt( iTemp, pRecStruct ); /// for remedies /* delete pRecStruct; pRecStruct = NULL;*/ bResult = true; //clip1++; } Thanks. Amrit Agrawal Software Developer.
-
Hi Developers, I was facing "Memory Leak" problem, from this chunk of code. I have allocate a memory for a structure( pRecStruct ) & after that it has stored in a map( CMapWordToPtr - pMapUserBoard ) for furher use. I have tried to delete as well, but the next time if i am trying to use. Plese tell me the issue. if( bSymAlreadyPresent == false ) { CString str(pDBStruct->Symp); strTemp.Format( _T("%d"), userClipIndx + 1 ); str += " Symptom Dropped on Clipboard " + strTemp; MessageBox( NULL, str, "", MB_OK ); NEWDB_STRUCT near *pRecStruct=NULL; pRecStruct=new NEWDB_STRUCT; int cLen=strlen(pDBStruct->Symp); pRecStruct->Symp=new char[cLen+1]; memset(pRecStruct->Symp, NULL,cLen+1); memcpy(pRecStruct->Symp,pDBStruct->Symp,cLen); pRecStruct->Remedy=new char[strlen(pDBStruct->Remedy)+1]; memset(pRecStruct->Remedy, NULL,strlen(pDBStruct->Remedy)+1); memcpy(pRecStruct->Remedy,pDBStruct->Remedy,strlen(pDBStruct->Remedy)); pRecStruct->RecSize=pDBStruct->RecSize; pRecStruct->LevelNum=pDBStruct->LevelNum; pRecStruct->RemedyCnt=pDBStruct->RemedyCnt; iTemp = pMapUsrClipboard->GetCount(); pRecStruct->SymNo = iTemp; pRecStruct->Author=pDBStruct->Author; pRecStruct->RecNo=pDBStruct->RecNo; pRecStruct->ParOffset=pDBStruct->ParOffset; pRecStruct->NextOffset=pDBStruct->NextOffset; pRecStruct->CurOffset=pDBStruct->CurOffset; pRecStruct->SymType=0; pMapUsrClipboard->SetAt( iTemp, pRecStruct ); /// for remedies /* delete pRecStruct; pRecStruct = NULL;*/ bResult = true; //clip1++; } Thanks. Amrit Agrawal Software Developer.
Amrit Agr wrote:
I have tried to delete as well, but the next time if i am trying to use.
That isn't clear. What do you mean? However you should know you need to delete the allocated character arrays together with the struct itself. You may use the destructor for this task:
struct RectStruct
{
//..
RectStruct()
{
Symp = NULL;
Remedy = NULL;
}
// you may also provide a ctor for the initialization of all the fields~RectStruct()
{
if (Symp) delete [] Symp;
if (Remedy) delelte [] Remedy;
}
};If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi Developers, I was facing "Memory Leak" problem, from this chunk of code. I have allocate a memory for a structure( pRecStruct ) & after that it has stored in a map( CMapWordToPtr - pMapUserBoard ) for furher use. I have tried to delete as well, but the next time if i am trying to use. Plese tell me the issue. if( bSymAlreadyPresent == false ) { CString str(pDBStruct->Symp); strTemp.Format( _T("%d"), userClipIndx + 1 ); str += " Symptom Dropped on Clipboard " + strTemp; MessageBox( NULL, str, "", MB_OK ); NEWDB_STRUCT near *pRecStruct=NULL; pRecStruct=new NEWDB_STRUCT; int cLen=strlen(pDBStruct->Symp); pRecStruct->Symp=new char[cLen+1]; memset(pRecStruct->Symp, NULL,cLen+1); memcpy(pRecStruct->Symp,pDBStruct->Symp,cLen); pRecStruct->Remedy=new char[strlen(pDBStruct->Remedy)+1]; memset(pRecStruct->Remedy, NULL,strlen(pDBStruct->Remedy)+1); memcpy(pRecStruct->Remedy,pDBStruct->Remedy,strlen(pDBStruct->Remedy)); pRecStruct->RecSize=pDBStruct->RecSize; pRecStruct->LevelNum=pDBStruct->LevelNum; pRecStruct->RemedyCnt=pDBStruct->RemedyCnt; iTemp = pMapUsrClipboard->GetCount(); pRecStruct->SymNo = iTemp; pRecStruct->Author=pDBStruct->Author; pRecStruct->RecNo=pDBStruct->RecNo; pRecStruct->ParOffset=pDBStruct->ParOffset; pRecStruct->NextOffset=pDBStruct->NextOffset; pRecStruct->CurOffset=pDBStruct->CurOffset; pRecStruct->SymType=0; pMapUsrClipboard->SetAt( iTemp, pRecStruct ); /// for remedies /* delete pRecStruct; pRecStruct = NULL;*/ bResult = true; //clip1++; } Thanks. Amrit Agrawal Software Developer.
Friend, ur question not clear. Tho i assume u face memory leak problem. and i suggeset watever u doing is rite. that is u delete the pointer and assign it to null. Arun P.
-
Hi Developers, I was facing "Memory Leak" problem, from this chunk of code. I have allocate a memory for a structure( pRecStruct ) & after that it has stored in a map( CMapWordToPtr - pMapUserBoard ) for furher use. I have tried to delete as well, but the next time if i am trying to use. Plese tell me the issue. if( bSymAlreadyPresent == false ) { CString str(pDBStruct->Symp); strTemp.Format( _T("%d"), userClipIndx + 1 ); str += " Symptom Dropped on Clipboard " + strTemp; MessageBox( NULL, str, "", MB_OK ); NEWDB_STRUCT near *pRecStruct=NULL; pRecStruct=new NEWDB_STRUCT; int cLen=strlen(pDBStruct->Symp); pRecStruct->Symp=new char[cLen+1]; memset(pRecStruct->Symp, NULL,cLen+1); memcpy(pRecStruct->Symp,pDBStruct->Symp,cLen); pRecStruct->Remedy=new char[strlen(pDBStruct->Remedy)+1]; memset(pRecStruct->Remedy, NULL,strlen(pDBStruct->Remedy)+1); memcpy(pRecStruct->Remedy,pDBStruct->Remedy,strlen(pDBStruct->Remedy)); pRecStruct->RecSize=pDBStruct->RecSize; pRecStruct->LevelNum=pDBStruct->LevelNum; pRecStruct->RemedyCnt=pDBStruct->RemedyCnt; iTemp = pMapUsrClipboard->GetCount(); pRecStruct->SymNo = iTemp; pRecStruct->Author=pDBStruct->Author; pRecStruct->RecNo=pDBStruct->RecNo; pRecStruct->ParOffset=pDBStruct->ParOffset; pRecStruct->NextOffset=pDBStruct->NextOffset; pRecStruct->CurOffset=pDBStruct->CurOffset; pRecStruct->SymType=0; pMapUsrClipboard->SetAt( iTemp, pRecStruct ); /// for remedies /* delete pRecStruct; pRecStruct = NULL;*/ bResult = true; //clip1++; } Thanks. Amrit Agrawal Software Developer.
Amrit Agr wrote:
I have tried to delete as well...
You've not shown any code that deletes.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
Amrit Agr wrote:
I have tried to delete as well...
You've not shown any code that deletes.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
I meant this in ur code. you hav commented this! delete pRecStruct; pRecStruct = NULL; on using this also you face the problem?.
Arun P.