Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Problem using C++ CString for file i/o

Options
  • 11-10-2006 1:11pm
    #1
    Registered Users Posts: 205 ✭✭


    I'm rebuilding a text file editing application which I originally wrote under Visual C++ 6.0 in the Visual C++ .NET environment. An exception has popped up when using the following code to allocate a CString buffer for the data read from the file. The code in question is as follows:

    LPTSTR pszBuffer = m_szDataBuffer.GetBuffer(ar.GetFile()->GetLength());
    m_nBufferLength = ar.Read( pszBuffer, ar.GetFile()->GetLength() );
    m_szDataBuffer.ReleaseBuffer();

    The call to m_szDataBuffer.ReleaseBuffer() throws an exception because the size of the buffer to be released is greater than the allocated buffer => ReleaseBuffer() calls SetLength(nLength) with nLength == the size of data from the file. The following Assert fails in SetLength:

    ATLASSERT( nLength <= GetData()->nAllocLength );

    GetData()->nAllocLength is less than half the size that should be allocated in the buffer.

    Any suggestions on how to proceed?


Comments

  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    As far as I recall the GetBuffer ( ) function can throw exceptions as well. Have you tried placing the call to GetBuffer in a try/catch block to see if any exception is being thrown. If only half of the desired length is being allocated, I'd say this function might be the cause.


  • Registered Users Posts: 205 ✭✭Stugots


    Thanks for your help. Turns out the problem was that I was not specifying the buffer length when releasing the buffer.


Advertisement