Solved

Bug in CByteArray::Serialize?

Posted on 2003-10-28
3
434 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now