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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Problem using C++ CString for file i/o

  • 11-10-2006 12: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