Schema Numbers Not Stored if Serialize() Called Directly

ID: Q118612

7.00 | 1.00 1.50 1.51 1.52 | 1.00 2.00 2.10 2.20 4.00 4.10 4.20

MS-DOS | WINDOWS             | WINDOWS NT
kbprg

The information in this article applies to:

SUMMARY

When you store an object in an archive by calling Serialize() directly, the schema number for the object is not stored. As a result, if an older version of an object with an older schema number is read using a later version of the object, a CArchiveExeception is not produced. Instead, the object reads the data that was written in the older format, using code that reads the newer format; this often fails, causing the application to crash.

MORE INFORMATION

MFC objects that use the DECLARE_SERIAL and IMPLEMENT_SERIAL macros have schema numbers that indicate the "version" of the object. An MFC archive maintains a list of objects and their schema numbers. When an object is stored by means of the insertion operator (<<) or the WriteObject() function, the schema number is also stored. Then, when the object is retrieved using the extraction operator (>>) or the ReadObject() function, the schema number from the archive is compared with the schema number of the object in the application. If they are different, a CArchiveException is produced.

When an object is stored by calling the Serialize() function of the object directly, the schema number is not stored. When an older version of an object is read back into the archive, Serialize() simply attempts to read the data from the archive using the newer code for the object. If the data format of the object has changed, the object is filled with bad data and most likely causes the application to crash.

One way to avoid this problem is to use the insertion and extraction operators, or ReadObject() and WriteObject(), whenever you serialize an object. However, there are cases where you cannot do this and need to call Serialize() directly. When you archive an object using the insertion and extraction operators, or ReadObject() and WriteObject(), MFC dynamically creates the object when it is loaded. If the memory for your object is already allocated when you are ready to load the object, you will not want MFC to create a new object, so you need to call Serialize() directly.

If you need to store an object by calling Serialize() directly, then you may want to have your object contain its own version number in its Serialize() function. While you are loading the object, check this number to verify that the versions are the same.

Additional reference words: kbinf 7.00 1.00 1.50 2.00 2.10 2.20 2.50 2.51 2.52 3.00 3.10 4.00 4.10 4.20 KBCategory: kbprg KBSubcategory: MfcFileIO

Keywords          : kbFileIO kbMFC kbVC 
Version           : 7.00 | 1.00 1.50 1.51 1.52 | 1
Platform          : MS-DOS NT WINDOWS

Last Reviewed: October 11, 1997