Serialization produces memory fault -- even in this TINY class! - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Serialization produces memory fault -- even in this TINY class!

__c4.ep__c4.ep Posts: 28Member
[b][red]This message was edited by __c4.ep at 2003-5-20 7:37:21[/red][/b][hr]
Alright, I've tried everything over the last 5 hours, but I just can't get this code to work.

What I want to do is to serialize a class in an SDI app (created with app wizard), which holds a list of another class holding a CString object. Here's the code:

[code]
// header

class COther
{
public:
CString m_str;
};

class CData
{
public:

void Serialize( CArchive& ar )
{
m_list.Serialize( ar );
}

CList m_list;
};
[/code]

Then I instantiate CData in CSerializationDoc and add an object of COther to the list in CSerializationDoc's ctor:

[code]
CSerializationDoc::CSerializationDoc()
{
// TODO: add one-time construction code here
COther obj;
obj.m_str = "Text";
m_data.m_list.AddTail( obj );
}
[/code]

Then I finally have my Serialization func:

[code]
void CSerializationDoc::Serialize(CArchive& ar)
{
m_data.Serialize( ar );
}
[/code]

Well, if I run this app and load a document, I'm getting memory faults when exiting the app. The errors occur in CStringData::Release().
Obviously, the app tries to release a CString which already HAS been released, thus trying to delete data where nothing is anymore.

Does anyone know what the heck is going wrong here?
I have already posted two times on codeguru.com but noone can help me there. Perhaps you can?


«13

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2003-5-20 9:33:34[/red][/b][hr]
    [blue]Interesting challenge if you don't know how to do it. Just need a couple minior changes[/blue]
    [code]
    other.h

    class COther
    {
    public:
    COther();
    virtual ~COther();
    [red] virtual void Serialize(CArchive& ar);[/red]
    CString m_str;
    };

    [/code]
    [code]
    other.cpp
    void COther::Serialize(CArchive& ar)
    {
    if(ar.IsStoring())
    ar.WriteString(m_str);
    else
    ar.ReadString(m_str);

    }
    [/code]
    [code]
    MyDoc.cpp

    template <> void AFXAPI SerializeElements ( CArchive& ar, COther* pNewOther, int nCount )
    {
    for ( int i = 0; i < nCount; i++, pNewOther++ )
    {
    // Serialize each COther object
    pNewOther->Serialize( ar );
    }
    }

    [/code]


  • __c4.ep__c4.ep Posts: 28Member
    : [blue]Interesting challenge if you don't know how to do it. Just need a couple minior changes[/blue]

    First of all, thanks for your quick answer, I'll try this out immediately.

    But frankly I still don't understand [b]why[/b] the error occured. Could you explain what exactly went wrong there? I racked my brain on this one the whole day and I still don't get why it didn't work.

    Thanks in advance.
  • stoberstober Posts: 9,765Member ✭✭✭
    : : [blue]Interesting challenge if you don't know how to do it. Just need a couple minior changes[/blue]
    :
    : First of all, thanks for your quick answer, I'll try this out immediately.
    :
    : But frankly I still don't understand [b]why[/b] the error occured. Could you explain what exactly went wrong there? I racked my brain on this one the whole day and I still don't get why it didn't work.
    :
    : Thanks in advance.
    :
    [blue]I suspect it was because the template class didn't know how to serialize that COther class. You have to tell it how with the SerialzeObjects() template I added to the document class. After I added that, all the problems disappeared.[/blue]

  • __c4.ep__c4.ep Posts: 28Member
    : [blue]I suspect it was because the template class didn't know how to serialize that COther class. You have to tell it how with the SerialzeObjects() template I added to the document class. After I added that, all the problems disappeared.[/blue]
    :
    Well, thanks a ton :)

  • __c4.ep__c4.ep Posts: 28Member
    Oh one more question real quick:

    When exactly do I have to use the DECLARE_SERIAL/IMPLEMENT_SERIAL macros? Actually my first thought was that I have to make my COther class known to the Serialization system using those macros.

    But obviously it works without them. *confused*

  • stoberstober Posts: 9,765Member ✭✭✭
    : Oh one more question real quick:
    :
    : When exactly do I have to use the DECLARE_SERIAL/IMPLEMENT_SERIAL macros? Actually my first thought was that I have to make my COther class known to the Serialization system using those macros.
    :
    : But obviously it works without them. *confused*
    :
    :
    [blue]In your case, it doesn't matter whether you use it or not. If you want to use it for some other reason, then CObject (or some other class derived from CObject) needs to be COther's base class.

    The macros are used when the type of object in the file is unknown until runtime. The file contains information about the class that was previously written there during serialzation using the << and >> CArchive operators. You don't need this feature because you know exactly what class the data in the file belongs to and because the CList class allocates memory for them before it calls COther class's serialize function.

    Clear as mud?
    [/blue]
  • __c4.ep__c4.ep Posts: 28Member
    : Clear as mud?

    Somewhat yeah. Sounds like a RTTI mechanism. Although RTTI is still a big [b]?[/b] for me, I think I basically got what you mean. :)
  • __c4.ep__c4.ep Posts: 28Member
    I'm currently trying to implement your solution but I'm still having problems:

    If I define that function template, the compiler tells me that SerializeElements has already been defined in another file:

    [i]SWGCB error LNK2005: "void __stdcall SerializeElements(class CArchive &,class CSkillMod *,int)" (?SerializeElements@@YGXAAVCArchive@@PAVCSkillMod@@H@Z) already defined in Character.obj[/i]

    The function is placed in my Document Class' cpp file.
  • stoberstober Posts: 9,765Member ✭✭✭
    : I'm currently trying to implement your solution but I'm still having problems:
    :
    : If I define that function template, the compiler tells me that SerializeElements has already been defined in another file:
    :
    : [i]SWGCB error LNK2005: "void __stdcall SerializeElements(class CArchive &,class CSkillMod *,int)" (?SerializeElements@@YGXAAVCArchive@@PAVCSkillMod@@H@Z) already defined in Character.obj[/i]
    :
    : The function is placed in my Document Class' cpp file.
    :
    [blue]I had a similary problem in the example I gave you earlier. So I just moved that function to the top of the file that the compiler complained about and it fixed it. You will probably have to do the same. Move the function to the top of Character.cpp.[/blue]
  • __c4.ep__c4.ep Posts: 28Member
    Now I'm getting the following error (it's the same I described in the email, don't know if you've already read it, I've sent it through this board):

    error C2908:
    explicit specialization; 'void SerializeElements(CArchive &,CSkill *,INT_PTR)' has already been instantiated from the primary template
«13
Sign In or Register to comment.