Solved

Bug in CByteArray::Serialize?

Posted on 2003-10-28
3
438 Views
Last Modified: 2013-11-20
Is there a bug in CByteArray::Serialize in Visual C++ .NET or have I misunderstood something?

It seems like when reading there is one ar.Read(pbData, nBytesToRead); to much. What can I do to correct this. Is it possible to recompile the C:\Program Files\Microsoft Visual Studio .NET\VC7\atlmfc\src\mfc\ array_b.cpp file?


// In Visual C++ 6.0 it works fine
void CByteArray::Serialize(CArchive& ar)
{
      ASSERT_VALID(this);

      CObject::Serialize(ar);

      if (ar.IsStoring())
      {
            ar.WriteCount(m_nSize);
            ar.Write(m_pData, m_nSize * sizeof(BYTE));
      }
      else
      {
            DWORD nOldSize = ar.ReadCount();
            SetSize(nOldSize);
            ar.Read(m_pData, m_nSize * sizeof(BYTE));
      }
}

// In Visual C++ .NET bug?
void CByteArray::Serialize(CArchive& ar)
{
      UINT_PTR nBytesLeft;
      UINT nBytesToWrite;
      UINT nBytesToRead;
      LPBYTE pbData;

      ASSERT_VALID(this);

      CObject::Serialize(ar);

      if (ar.IsStoring())
      {
            ar.WriteCount(m_nSize);
            nBytesLeft = m_nSize*sizeof(BYTE);
            pbData = m_pData;
            while(nBytesLeft > 0)
            {
                  nBytesToWrite = UINT(min(nBytesLeft, INT_MAX));
                  ar.Write(pbData, nBytesToWrite);
                  pbData += nBytesToWrite;
                  nBytesLeft -= nBytesToWrite;
            }
      }
      else
      {
            DWORD_PTR nOldSize = ar.ReadCount();
            SetSize(nOldSize);
            nBytesLeft = m_nSize*sizeof(BYTE);
            pbData = m_pData;
            while(nBytesLeft > 0)
            {
                  nBytesToRead = UINT(min(nBytesLeft, INT_MAX));
                  ar.Read(pbData, nBytesToRead);
                  pbData += nBytesToRead;
                  nBytesLeft -= nBytesToRead;
                  ar.Read(pbData, nBytesToRead); ////////////////////////////////////////////////////////Should be removed?
            }
      }
}
0
Comment
Question by:P1R
  • 2
3 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9632838
Serialize is virtual function, you can derive your own class from CByteArray and overwrite the Serialize function. You can test by this way your correction to Serialize function.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 100 total points
ID: 9632844
0
 

Author Comment

by:P1R
ID: 9633333
Thanks AlexFM. I didn't expect to be the first to find this bug... The workaround works fine. It's nice to know that (for once?) it is Microsoft and not I that have made a mistake:-)
 
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Looking for a specific application/software 2 114
either24  challenge 19 101
mapBully challenge 6 152
Authenticate using sesu from script 7 56
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question