Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Problem using C++ CString for file i/o

  • 11-10-2006 01:11PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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