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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Memory Leak

Memory Leak

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancehelptutorial
6 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.
  • S Offline
    S Offline
    saravanaram
    wrote on last edited by
    #1

    Hi all, in mfc dialog based application i have the following function. void Reverse() { // TODO: Add your control notification handler code here int nIndex, nStrLength, nWordIndex; TCHAR *pStrWord, *pStrFinal, ch; UpdateData(TRUE); nStrLength = m_strInput.GetLength(); pStrWord = new TCHAR [nStrLength + 1]; pStrFinal = new TCHAR [nStrLength + 1]; *pStrWord = '\0'; *pStrFinal = '\0'; nWordIndex = 0; for (nIndex = 0;nIndex < nStrLength; nIndex++) { ch = m_strInput.GetAt(nIndex); if ( ch != ' ' || nIndex == nStrLength ) { pStrWord[nWordIndex] = m_strInput.GetAt(nIndex); nWordIndex++; } if ( ch == ' ' ) { pStrWord[nWordIndex++] = ' '; pStrWord[nWordIndex] = '\0'; _tcscat(pStrFinal,_tcsrev(pStrWord)); nWordIndex=0; } if ( nIndex == (nStrLength - 1) ) { pStrWord[nWordIndex++] = ' '; pStrWord[nWordIndex] = '\0'; _tcscat(pStrFinal,_tcsrev(pStrWord)); nWordIndex = 0; } } pStrFinal = _tcsrev(pStrFinal); pStrFinal[nStrLength] = '\0'; m_strOutput = pStrFinal; UpdateData(FALSE); delete [] pStrWord; delete [] pStrFinal; } I get error when deleting pStrFinal could anyone tell me why this happens. can any one of you explain me how to dump memory and to verify it.

    S H D 3 Replies Last reply
    0
    • S saravanaram

      Hi all, in mfc dialog based application i have the following function. void Reverse() { // TODO: Add your control notification handler code here int nIndex, nStrLength, nWordIndex; TCHAR *pStrWord, *pStrFinal, ch; UpdateData(TRUE); nStrLength = m_strInput.GetLength(); pStrWord = new TCHAR [nStrLength + 1]; pStrFinal = new TCHAR [nStrLength + 1]; *pStrWord = '\0'; *pStrFinal = '\0'; nWordIndex = 0; for (nIndex = 0;nIndex < nStrLength; nIndex++) { ch = m_strInput.GetAt(nIndex); if ( ch != ' ' || nIndex == nStrLength ) { pStrWord[nWordIndex] = m_strInput.GetAt(nIndex); nWordIndex++; } if ( ch == ' ' ) { pStrWord[nWordIndex++] = ' '; pStrWord[nWordIndex] = '\0'; _tcscat(pStrFinal,_tcsrev(pStrWord)); nWordIndex=0; } if ( nIndex == (nStrLength - 1) ) { pStrWord[nWordIndex++] = ' '; pStrWord[nWordIndex] = '\0'; _tcscat(pStrFinal,_tcsrev(pStrWord)); nWordIndex = 0; } } pStrFinal = _tcsrev(pStrFinal); pStrFinal[nStrLength] = '\0'; m_strOutput = pStrFinal; UpdateData(FALSE); delete [] pStrWord; delete [] pStrFinal; } I get error when deleting pStrFinal could anyone tell me why this happens. can any one of you explain me how to dump memory and to verify it.

      S Offline
      S Offline
      Stephane Rodriguez
      wrote on last edited by
      #2

      With pStrFinal = _tcsrev(pStrFinal);, you are messing your pointer away. I recommend to use the MFC CString class instead (since you said it's a MFC dialog). This hides all this memory allocation mess.

      1 Reply Last reply
      0
      • S saravanaram

        Hi all, in mfc dialog based application i have the following function. void Reverse() { // TODO: Add your control notification handler code here int nIndex, nStrLength, nWordIndex; TCHAR *pStrWord, *pStrFinal, ch; UpdateData(TRUE); nStrLength = m_strInput.GetLength(); pStrWord = new TCHAR [nStrLength + 1]; pStrFinal = new TCHAR [nStrLength + 1]; *pStrWord = '\0'; *pStrFinal = '\0'; nWordIndex = 0; for (nIndex = 0;nIndex < nStrLength; nIndex++) { ch = m_strInput.GetAt(nIndex); if ( ch != ' ' || nIndex == nStrLength ) { pStrWord[nWordIndex] = m_strInput.GetAt(nIndex); nWordIndex++; } if ( ch == ' ' ) { pStrWord[nWordIndex++] = ' '; pStrWord[nWordIndex] = '\0'; _tcscat(pStrFinal,_tcsrev(pStrWord)); nWordIndex=0; } if ( nIndex == (nStrLength - 1) ) { pStrWord[nWordIndex++] = ' '; pStrWord[nWordIndex] = '\0'; _tcscat(pStrFinal,_tcsrev(pStrWord)); nWordIndex = 0; } } pStrFinal = _tcsrev(pStrFinal); pStrFinal[nStrLength] = '\0'; m_strOutput = pStrFinal; UpdateData(FALSE); delete [] pStrWord; delete [] pStrFinal; } I get error when deleting pStrFinal could anyone tell me why this happens. can any one of you explain me how to dump memory and to verify it.

        H Offline
        H Offline
        Hari Krishnan Noida
        wrote on last edited by
        #3

        Hi, Regarding the technique of dumping in-memory statistics, is given below Note: memstate1 takes snapshot of pre-memory leak and memstate2 takes snapshot of post-memory leak. finally, memstate3 makes the statistics based on both the snapshots. CMemoryState memState1, memState2, memState3; void CHello::MakeMemoryLeak() { memState1.Checkpoint(); LPCTSTR strMemoryLeak = new char[50]; memState2.Checkpoint(); memState3.Difference(memState1, memState2); memState3.DumpStatistics(); } Hope this Helps Regards ~Hari~

        1 Reply Last reply
        0
        • S saravanaram

          Hi all, in mfc dialog based application i have the following function. void Reverse() { // TODO: Add your control notification handler code here int nIndex, nStrLength, nWordIndex; TCHAR *pStrWord, *pStrFinal, ch; UpdateData(TRUE); nStrLength = m_strInput.GetLength(); pStrWord = new TCHAR [nStrLength + 1]; pStrFinal = new TCHAR [nStrLength + 1]; *pStrWord = '\0'; *pStrFinal = '\0'; nWordIndex = 0; for (nIndex = 0;nIndex < nStrLength; nIndex++) { ch = m_strInput.GetAt(nIndex); if ( ch != ' ' || nIndex == nStrLength ) { pStrWord[nWordIndex] = m_strInput.GetAt(nIndex); nWordIndex++; } if ( ch == ' ' ) { pStrWord[nWordIndex++] = ' '; pStrWord[nWordIndex] = '\0'; _tcscat(pStrFinal,_tcsrev(pStrWord)); nWordIndex=0; } if ( nIndex == (nStrLength - 1) ) { pStrWord[nWordIndex++] = ' '; pStrWord[nWordIndex] = '\0'; _tcscat(pStrFinal,_tcsrev(pStrWord)); nWordIndex = 0; } } pStrFinal = _tcsrev(pStrFinal); pStrFinal[nStrLength] = '\0'; m_strOutput = pStrFinal; UpdateData(FALSE); delete [] pStrWord; delete [] pStrFinal; } I get error when deleting pStrFinal could anyone tell me why this happens. can any one of you explain me how to dump memory and to verify it.

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

          saravanaram wrote: I get error when deleting pStrFinal... So what's the error? I suspect the problem lies within the following line, but can't know for sure without stepping through the code. pStrFinal = _tcsrev(pStrFinal);

          H 1 Reply Last reply
          0
          • D David Crow

            saravanaram wrote: I get error when deleting pStrFinal... So what's the error? I suspect the problem lies within the following line, but can't know for sure without stepping through the code. pStrFinal = _tcsrev(pStrFinal);

            H Offline
            H Offline
            Hari Krishnan Noida
            wrote on last edited by
            #5

            Hi David, if you see the memory contents of (pStrWord), it contains 5 (CD - valid memory allocated) and 4 FD(padded memory, # may differ) (Note: Assume that nStrLength is 5) Following code lines, are writing on unallocated memory. pStrWord[nWordIndex++] = ' '; //This causes the problem. pStrWord[nWordIndex] = '\0'; _tcscat(pStrFinal,_tcsrev(pStrWord)); as well, pStrFinal = _tcsrev(pStrFinal); //This assignment also causes the problem. Actually, he has to allocate the extra one byte of memory to put the space character (' '). Hopefully, I am right. regards ~Hari~

            D 1 Reply Last reply
            0
            • H Hari Krishnan Noida

              Hi David, if you see the memory contents of (pStrWord), it contains 5 (CD - valid memory allocated) and 4 FD(padded memory, # may differ) (Note: Assume that nStrLength is 5) Following code lines, are writing on unallocated memory. pStrWord[nWordIndex++] = ' '; //This causes the problem. pStrWord[nWordIndex] = '\0'; _tcscat(pStrFinal,_tcsrev(pStrWord)); as well, pStrFinal = _tcsrev(pStrFinal); //This assignment also causes the problem. Actually, he has to allocate the extra one byte of memory to put the space character (' '). Hopefully, I am right. regards ~Hari~

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

              Good observation.

              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