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 with CString

Memory leak with CString

Scheduled Pinned Locked Moved C / C++ / MFC
performancehelp
4 Posts 3 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.
  • M Offline
    M Offline
    Manoj Kumar Rai
    wrote on last edited by
    #1

    I have found a very interesting incident of memory leak in my code. Although I have not been able to find the root cause of the leak still thought to share with you all. I will really appreciate any help on this. I am rather curious to know how come a memory leak can be seen in use of CString in such a simple way. Code: We have a thread function:

    UINT __cdecl LogWriteThread(void *pParam)
    {
    CLog* pLog = (CLog*)pParam;
    UNUSED_ALWAYS(pParam);
    try
    {
    pLog->LogWriteFunc();
    }
    .................
    .................
    return 0;
    }

    UINT __cdecl CLog::LogWriteFunc(void *pParam)
    {
    ..........
    CString cstrLogDataBuffer = m_cstrLogLine;
    m_cstrLogLine = _T("");
    CopyLogToFileByThread(cstrLogDataBuffer);
    }

    void CMpmLog::CopyLogToFileByThread(const CString& cstrLogDataBuffer)
    {
    ....................
    const long iLength = cstrLogDataBuffer.GetLength();
    char *pBuffer = new char[iLength + 1];
    for (long iIndex = 0;iIndex < iLength; iIndex++)
    {
    pBuffer[iIndex] = (char)cstrLogDataBuffer.GetAt(iIndex);
    }
    pBuffer[iLength] = '\0';

      const unsigned int uiWriteLen = fwrite((const void \*)pBuffer, sizeof(char),iLength, m\_pofLogStream);
    
      delete\[\] pBuffer;
      ....................
    

    }

    ======================== If I commment call for "CopyLogToFileByThread" in function LogWriteFunc then memory leak goes away. But thats not the solution.

    Manoj Never Gives up

    J D 2 Replies Last reply
    0
    • M Manoj Kumar Rai

      I have found a very interesting incident of memory leak in my code. Although I have not been able to find the root cause of the leak still thought to share with you all. I will really appreciate any help on this. I am rather curious to know how come a memory leak can be seen in use of CString in such a simple way. Code: We have a thread function:

      UINT __cdecl LogWriteThread(void *pParam)
      {
      CLog* pLog = (CLog*)pParam;
      UNUSED_ALWAYS(pParam);
      try
      {
      pLog->LogWriteFunc();
      }
      .................
      .................
      return 0;
      }

      UINT __cdecl CLog::LogWriteFunc(void *pParam)
      {
      ..........
      CString cstrLogDataBuffer = m_cstrLogLine;
      m_cstrLogLine = _T("");
      CopyLogToFileByThread(cstrLogDataBuffer);
      }

      void CMpmLog::CopyLogToFileByThread(const CString& cstrLogDataBuffer)
      {
      ....................
      const long iLength = cstrLogDataBuffer.GetLength();
      char *pBuffer = new char[iLength + 1];
      for (long iIndex = 0;iIndex < iLength; iIndex++)
      {
      pBuffer[iIndex] = (char)cstrLogDataBuffer.GetAt(iIndex);
      }
      pBuffer[iLength] = '\0';

        const unsigned int uiWriteLen = fwrite((const void \*)pBuffer, sizeof(char),iLength, m\_pofLogStream);
      
        delete\[\] pBuffer;
        ....................
      

      }

      ======================== If I commment call for "CopyLogToFileByThread" in function LogWriteFunc then memory leak goes away. But thats not the solution.

      Manoj Never Gives up

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      The shown code part of CopyLogToFileByThread() does not change a CString object. So this can't be the source of the leak it is from a CString object. It seems that you are accessing the m_strLogLine CString object by multiple threads. When doing so, you must use locking. To do so, you may add a CCriticalSection member to your class and use this for locking:

      CSingleLock singleLock(&m_CritSection);
      singleLock.Lock();
      CString cstrLogDataBuffer = m_cstrLogLine;
      m_cstrLogLine = _T("");
      singleLock.Lock();

      Use similar code when changing or reading m_strLogLine anywhere. One more note: Instead of converting a TCHAR string to char by casting each char, you may use WideCharToMultiByte() or the conversion provided by CString:

      // This converts a TCHAR string to char string
      CStringA strLineA(cstrLogDataBuffer.GetString());
      fwrite(strLineA.GetString(), sizeof(char), strLineA.GetLength(), m_pofLogStream);

      M 1 Reply Last reply
      0
      • J Jochen Arndt

        The shown code part of CopyLogToFileByThread() does not change a CString object. So this can't be the source of the leak it is from a CString object. It seems that you are accessing the m_strLogLine CString object by multiple threads. When doing so, you must use locking. To do so, you may add a CCriticalSection member to your class and use this for locking:

        CSingleLock singleLock(&m_CritSection);
        singleLock.Lock();
        CString cstrLogDataBuffer = m_cstrLogLine;
        m_cstrLogLine = _T("");
        singleLock.Lock();

        Use similar code when changing or reading m_strLogLine anywhere. One more note: Instead of converting a TCHAR string to char by casting each char, you may use WideCharToMultiByte() or the conversion provided by CString:

        // This converts a TCHAR string to char string
        CStringA strLineA(cstrLogDataBuffer.GetString());
        fwrite(strLineA.GetString(), sizeof(char), strLineA.GetLength(), m_pofLogStream);

        M Offline
        M Offline
        Manoj Kumar Rai
        wrote on last edited by
        #3

        Thanks Jochen for your inputs. 1. I have used CriticalSection while accessing m_cstrLogLine but have omitted those additional lines to focus on actual business logic. 2. I agree that WideCharToMultiByte() could have been a better option to convert string but I have changed code to rule out any leak during conversion itself. 3. The CopyLogToFileByThread() is not modifying string and thats why its surprising to have memory leak from that function. 4. One more point i will like to mention here is thhat this application has been built for WinCE 6.0. I am trying to replicate the same issue with a test application and if possible for XP.

        Manoj Never Gives up

        1 Reply Last reply
        0
        • M Manoj Kumar Rai

          I have found a very interesting incident of memory leak in my code. Although I have not been able to find the root cause of the leak still thought to share with you all. I will really appreciate any help on this. I am rather curious to know how come a memory leak can be seen in use of CString in such a simple way. Code: We have a thread function:

          UINT __cdecl LogWriteThread(void *pParam)
          {
          CLog* pLog = (CLog*)pParam;
          UNUSED_ALWAYS(pParam);
          try
          {
          pLog->LogWriteFunc();
          }
          .................
          .................
          return 0;
          }

          UINT __cdecl CLog::LogWriteFunc(void *pParam)
          {
          ..........
          CString cstrLogDataBuffer = m_cstrLogLine;
          m_cstrLogLine = _T("");
          CopyLogToFileByThread(cstrLogDataBuffer);
          }

          void CMpmLog::CopyLogToFileByThread(const CString& cstrLogDataBuffer)
          {
          ....................
          const long iLength = cstrLogDataBuffer.GetLength();
          char *pBuffer = new char[iLength + 1];
          for (long iIndex = 0;iIndex < iLength; iIndex++)
          {
          pBuffer[iIndex] = (char)cstrLogDataBuffer.GetAt(iIndex);
          }
          pBuffer[iLength] = '\0';

            const unsigned int uiWriteLen = fwrite((const void \*)pBuffer, sizeof(char),iLength, m\_pofLogStream);
          
            delete\[\] pBuffer;
            ....................
          

          }

          ======================== If I commment call for "CopyLogToFileByThread" in function LogWriteFunc then memory leak goes away. But thats not the solution.

          Manoj Never Gives up

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

          Manoj Kumar Rai wrote:

          I have found a very interesting incident of memory leak in my code.

          How are you verifying this?

          "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

          "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

          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