INF: Archiving Several Objects Pointing to a Third Object

ID Number: Q85520

1.00 | 1.00

MS-DOS | WINDOWS

Summary:

When using the archiving capabilities of the Microsoft Foundation

Classes version 1.0, which shipped with the Microsoft C/C++ compiler

version 7.0, special care must be taken when archiving objects

containing pointers. If two distinct objects (A and B) both point to a

third object Ó, and are serialized into separate archives, when

these objects are deserialized they will each point to their own

unique copy of object C.

This may cause several problems. If object C can be edited by either

object A or B, the edit will affect only one of the copies of C and

inconsistencies may result. In addition, duplicates of the original

object C now exist on disk and in memory, wasting system resources.

More Information:

The CArchive class provides a solution to this classic problem when

the first two objects (A and B) are serialized into the same archive

object. Each archive object maintains a mapping from a stored object

to a 16-bit persistent identifier (PID). Each unique object and class

name within an archive is assigned a new PID.

If a request is made to save an object to the archive (usually through

the insertion operator), a check is made to see if the class or object

has already been archived. For any class or object already saved, the

existing PID associated with that object is inserted, rather than a

copy of the class or object.

Because the PIDs have no significance outside an archive, if objects A

and B are serialized into different archives, object C will be

assigned a PID and copied into each archive, even if both archives

refer to the same file. Even if you disregard the wasted space,

deserializing the objects results in a data structure containing two

distinct copies of object C, and does not reflect the original data

structure.

More information on storage of objects using the CArchive class can be

found in the Microsoft Foundation Classes technical note TN002.TXT,

the "Microsoft C/C++ Class Libraries User's Guide," and the "Microsoft

C/C++ Class Libraries Reference."

Additional reference words: 7.00 CArchive persistent serialize