Microsoft KB Archive/132079

= HOWTO: How to Access CMemFile Directly When Using It with CArchive =

Article ID: 132079

Article Last Modified on 12/2/2003

-

APPLIES TO


 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 4.1 Subscription
 * Microsoft Foundation Class Library 4.2
 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 2.1
 * Microsoft Visual C++ 2.2

-



This article was previously published under Q132079



SUMMARY
When using CMemFile in conjunction with CArchive, problems may arise from calling CArchive::GetFile and subsequently using the CFile pointer returned for direct file access.

This is true for CFile and any class derived from CFile. Because CArchive buffers I/O, you need to call CArchive::Flush before using any of the members of CFile.



MORE INFORMATION
When a CMemFile object is used with a CArchive object, the two objects share a single buffer. Both objects retain separate pointers to this shared buffer. It is possible for the two objects to become out of synch if the file is accessed directly through a file pointer retrieved by calling CArchive::GetFile.

For example, say you open a CMemFile, and start writing to it using the serialization functions. Then, suppose you call CMemFile::GetFile, and write to the file using CFile::Write. The first thing that the CMemFile object does is make sure it has enough space for the pending write. For this example, assume that there is not enough space to satisfy the write. This means that the CMemFile object must reallocate the buffer. This will most likely change the location of the buffer. The problem occurs when you write to the CMemFile again through the CArchive object. The CArchive object still has a pointer to the old buffer, because the object has no way of knowing that the CMemFile reallocated the shared buffer. Any attempt to write through this pointer does not write to the appropriate file. Therefore, the data is missing from the file where it should have been written.

Additional query words: kbinf 1.00 1.50 2.00 2.10 2.50 2.51 2.52 3.00 3.10 3.20 2.20 4.00 4.10

Keywords: kbhowto kbfileio KB132079

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.