[ACCEPTED]-What if I don't call ReleaseBuffer after GetBuffer?-getbuffer

Accepted answer
Score: 11

I'm not sure that this will cause a memory 4 leak, but you must call ReleaseBuffer to ensure that 3 the private members of CString are updated. For 2 example, ReleaseBuffer will update the length field of 1 the CString by looking for the terminating null character.

Score: 3

What will happen if I don't use ReleaseBuffer() after GetBuffer()?

I 8 haven't used MFC (and hopefully won't ever 7 have to touch it with a ten-foot pole) but, as 6 a rule of thumb, whenever you have an API 5 that has both GetXXX() and ReleaseXXX() (especially when the 4 result of GetXXX() conveniently is of the type that 3 ReleaseXXX() takes) -- then when you forget to call 2 ReleaseXXX() for every one of your GetXXX() calls, you will 1 leak an XXX.

Score: 0

Here's an example of how I used CString::GetBuffer() and CString::ReleaseBuffer() :

LPTSTR pUnitBuffer = pAPBElement->m_strUnits.GetBuffer(APB_UNIT_SIZE);
if (pUnitBuffer != "")
{
   if (strncmp(pAPBElement->m_strUnits, (char*)pszBuffer[nLoop - nFirst], APB_UNIT_SIZE) != 0)
   {    
     LPTSTR pUnitOriginal = pAPBElement->m_strOriginal.GetBuffer(APB_UNIT_SIZE);

     strncpy(pUnitBuffer, 
            (char*)&pszBuffer[nLoop - nFirst], 
            APB_UNIT_SIZE);

     strncpy(pUnitOriginal, 
            (char*)&pszBuffer[nLoop - nFirst], 
            APB_UNIT_SIZE);

     pAPBElement->m_strOriginal.ReleaseBuffer();
    }
}
pAPBElement->m_strUnits.ReleaseBuffer();

0

Score: 0

If you do not modify the contents of the 2 CString using the pointer obtained using 1 GetBuffer(), you do NOT need to call ReleaseBuffer() afterwards

More Related questions