Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Memory Leak Problem

Memory Leak Problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpperformance
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Amrit Agr
    wrote on last edited by
    #1

    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.

    C A D 3 Replies Last reply
    0
    • A Amrit Agr

      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.

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      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]

      1 Reply Last reply
      0
      • A Amrit Agr

        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.

        A Offline
        A Offline
        Arun Parthasarathy
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A Amrit Agr

          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.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          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

          A 1 Reply Last reply
          0
          • D David Crow

            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

            A Offline
            A Offline
            Arun Parthasarathy
            wrote on last edited by
            #5

            I meant this in ur code. you hav commented this! delete pRecStruct; pRecStruct = NULL; on using this also you face the problem?.

            Arun P.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups