GetObjectSchema returns 0xffffffff

I have a document CMyDoc from CDocument

I've added the following line to the top of the .cpp file:

IMPLEMENT_SERIAL(MyDoc, CDocument, VERSIONABLE_SCHEMA|4)

And the following to the .h file:

DECLARE_SERIAL(CMyDoc)

In the CDocument serialize below, I always get 0xffffffff from GetObjectSchema. Why?

void CMyDoc::Serialize(CArchive& ar)
{
      //This function passes the archive on to the Network group
      //which in turn passes the archive to other lists & objects.
      
      if (ar.IsStoring())
      {
            ar << m_pNetwork;
      }
      else
      {
            UINT version = ar.GetObjectSchema();

            switch(version)
            {
            case 4:
                  ar >> m_pNetwork;
                  break;

            default:      
                                             //ALWAYS GOES HERE!!! WHY??      
                                   break;
            }
      }
}
JoyBurkeAsked:
Who is Participating?
 
jkrCommented:
You mean like "see above"? :o)
0
 
jkrCommented:
>>I always get 0xffffffff from GetObjectSchema. Why?

A return value of (UINT)–1 indicates that the version number is unknown. You need to specify the schema in 'IMPLEMENT_SERIAL()', e.g.

IMPLEMENT_SERIAL( CMyDoc, CDocument, 4 )


0
 
JoyBurkeAuthor Commented:
Jkr,

As I stated in my post, I have:

 IMPLEMENT_SERIAL(MyDoc, CDocument, VERSIONABLE_SCHEMA|4)

in MyDoc.cpp
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
JoyBurkeAuthor Commented:
The "m_pNetwork" is a pointer derived from CObject. It saves the version just fine with the Implement_Serial stuff I put in.

It's only the Doc that's giving me trouble. Is there something special I need to do in an MDI app to make it work?
0
 
jkrCommented:
>> The "m_pNetwork" is a pointer derived from CObject

Um, saving a pointer is kinda 'odd', since the next time you want to load the document, it's quite unlikely that you'll find an instance of the same class at the memory location you saved previously...
0
 
JoyBurkeAuthor Commented:
Well, if you serialize a pointer to a CObject, it calls the "serialize" for that object. It does not store the pointer directly.
0
 
JoyBurkeAuthor Commented:
But it really doesn't matter what I store

I've tried a simple int as a test.

It just doesn't get the version.
0
 
JoyBurkeAuthor Commented:
Originally, the Document contained:

IMPLEMENT_DYNCREATE(CMyDoc, CDocument)

I changed this to:

IMPLEMENT_SERIAL(CMyDoc, CDocument, VERSIONABLE_SCHEMA|4)

Does the problem have something to do with that? The CObject that I can get to serialize fine did not have this line originally.
0
 
jkrCommented:
Have you tried to use both 'IMPLEMENT_DYNCREATE()' and 'IMPLEMENT_SERIAL()'? The 1st one is needed for CObject support anyway.
0
 
JoyBurkeAuthor Commented:
Can't do that... I get a compile error

Why do I need 'IMPLEMENT_DYNCREATE()' for CObjects?
0
 
jkrCommented:
Well, as the docs put it:

Use the IMPLEMENT_DYNCREATE macro with the DECLARE_DYNCREATE macro to enable objects of CObject-derived classes to be created dynamically at run time. The framework uses this ability to create new objects dynamically, for example, when it reads an object from disk during serialization

BTW, you also added 'DECLARE_DYNCREATE()'?
0
 
JoyBurkeAuthor Commented:
OK -- I found the answer. "Serialize" is essentually called directly by the Save of the document. When called directly, the Schema will not be saved. The solution is to call

GetObjectSchema() only works if the object has been serialized with the CArchive &lt;&lt; operator. If the Serialize method is called directly the class schema is not saved.

You can force the Schema value to be stored by using SerializeClass() as follows.

void CMyDoc::Serialize(CArchive& ar)
{
ar.SerializeClass(RUNTIME_CLASS(CMyDoc));
if (ar.IsStoring())
{
// TODO: add storing code here

}
else
{
// TODO: add loading code here

int nVersion = ar.GetObjectSchema();
switch(nVersion)
{
case 1:
...
break;

...
...

}
}
}

Thanks for all your help. Just say "see fount above" and I'll accept your answer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.