PRB: CString::ReleaseBuffer() Does Not Release Extra Memory (114201)



The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), when used with:
    • Microsoft Visual C++ for Windows, 16-bit edition 1.0
    • Microsoft Visual C++ for Windows, 16-bit edition 1.5
    • Microsoft Visual C++ for Windows, 16-bit edition 1.51
    • Microsoft Visual C++ for Windows, 16-bit edition 1.52
    • Microsoft Visual C++, 32-bit Editions 1.0
    • Microsoft Visual C++, 32-bit Editions 2.0
    • Microsoft Visual C++, 32-bit Editions 2.1
    • Microsoft Visual C++, 32-bit Editions 4.0

This article was previously published under Q114201

SYMPTOMS

When CString::ReleaseBuffer is called and the length of the string is less than the allocated buffer length, the extra bytes are not released. This just means that your program might end up maintaining more memory than absolutely necessary. This will not cause a memory leak. All of the memory will be freed when the CString object is destroyed.

CAUSE

The behavior of CString::ReleaseBuffer described above is by design.

RESOLUTION

Visual C++ 2.0 and later include CString::FreeExtra to release any unused memory.

In earlier versions, one way to make the string's buffer length equal to the string's data length is to write a function which will free the extra memory. The two functions in the sample code section demonstrate two different methods of writing this function; either as a member function of a derived CString class or as a separate function which accepts a CString parameter type. Note that if the member function approach is taken, then the derived class will need to override the constructors provided by the CString class in order to have access to them.

MORE INFORMATION

By not freeing the extra bytes of memory in ReleaseBuffer, CString provides a simple method of growing without having to allocate more memory while keeping memory fragmentation to a minimum. For example, calling ReleaseBuffer(20) when the string's allocated buffer length is 200 bytes will leave an extra 180 bytes free. This way, the only time that a CString object will need to allocate more memory is when the data length exceeds the allocated buffer length.

Sample Code

  • Member function method
    class CMyString : public CString
    {
      public:
        CMyString(const char* psz = NULL) : CString(psz) {}
        void FreeExMem();
    };
    
    void CMyString::FreeExMem()
    {
      ASSERT(m_nDataLength<=m_nAllocLength);
      if(!IsEmpty())
      {
        char *tp = new char[m_nDataLength+1];
        memcpy(tp,m_pchData,m_nDataLength+1);
        ASSERT(m_pchData[m_nDataLength]=='\0');
        delete m_pchData;
        m_pchData = tp;
        m_nAllocLength = m_nDataLength;
      }
      ASSERT(m_pchData!=NULL);
    }
    						
  • Separate Function Method
    void FreeExMem(CString &s)
    {
      if(!s.IsEmpty())
      {
        char *p = s.GetBuffer(1);
        char *tp = new char[s.GetLength()+1];
        memcpy(tp,p,s.GetLength()+1);
        s.ReleaseBuffer();
        s.Empty();
        s = tp;
        delete tp;
      }
    }
    						

Modification Type:MajorLast Reviewed:12/1/2003
Keywords:kbBug kbprb kbVC500fix KB114201