Menu

crash problem when using SerializableObject with std::vector

Help
ollydbg
2013-10-23
2013-10-25
  • ollydbg

    ollydbg - 2013-10-23

    Hi, today, I have add the serialization feature to my own class, and I found that some times, my program crash, narrow down the crash issue, I found that the SerializableObject can not be used in std::vector, here is an example (modified code from SimpleListSample sample code)

    int main( int argc, char ** argv )
    {
        // create instance of XML serializer
        wxXmlSerializer Serializer;
    
        std::vector<SerializableObject> v;
    
        // first, create set of serializable class objects and add them to the serializer
        for( int i = 0; i < 5; i++ )
        {
            // Add all new class objects to the serializer's root ( pointer to parent object is NULL )
            // so the instances will be arranged into a list. Note that each serializable object
            // could be assigned as a child to another one so the objects could be arranged into a tree
            // structure as well.
            SerializableObject *p = new SerializableObject();
            Serializer << p;
            v.push_back(*p);
            // also member function of wxXmlSerializer could be used for this task as follows:
            //Serializer.AddItem( (xsSerializable*)NULL, new SerializableObject() );
        }
    
        // store the serializer's content to an XML file
        Serializer.SerializeToXml( wxT("data.xml") );
    
        // clear the serializer's content
        Serializer.RemoveAll();
    
        for( int i = 0; i < 5; i++ )
        {
            v.erase(v.begin());
        }
    

    Note, in my code, I use a vector to hold SerializableObject, the code above always crash on the second run of the for loop, the crash stack looks below:

    [debug]> bt 30
    [debug]#0  0x6734ad19 in wxmsw28u_xs!_ZN18wxPropertyListNode10DeleteDataEv () from D:\new_opencv_wxWidgets\wxxs-code-112-trunk\lib\gcc_dll\wxmsw28u_xs.dll
    [debug]#1  0x627305d7 in wxListBase::DoDeleteNode(wxNodeBase*) () from E:\code\wx-mingw-build-481-dw2\wxWidgets-2.8.12\lib\gcc_dll\wxmsw28u_gcc_custom.dll
    [debug]#2  0x6273082a in wxListBase::Clear() () from E:\code\wx-mingw-build-481-dw2\wxWidgets-2.8.12\lib\gcc_dll\wxmsw28u_gcc_custom.dll
    [debug]#3  0x6734ae46 in wxmsw28u_xs!_ZN14xsSerializableD2Ev () from D:\new_opencv_wxWidgets\wxxs-code-112-trunk\lib\gcc_dll\wxmsw28u_xs.dll
    [debug]#4  0x00403e8b in wxString::~wxString (this=0xb4e3f0, __in_chrg=<optimized out>) at E:\code\wx-mingw-build-481-dw2\wxWidgets-2.8.12\include\wx\string.h:659
    [debug]#5  0x00403cc4 in SerializableObject::~SerializableObject (this=0xb4e398, __in_chrg=<optimized out>) at D:\new_opencv_wxWidgets\wxxs-code-112-trunk\samples\Sample5\main.cpp:37
    [debug]#6  0x00403f0c in __gnu_cxx::new_allocator<SerializableObject>::destroy (this=0x22fe78, __p=0xb4e398) at d:\mingw-builds\i686-4.8.2-release-posix-dwarf-rt_v3-rev0\mingw32\lib\gcc\i686-w64-mingw32\4.8.2\include\c++\ext\new_allocator.h:133
    [debug]#7  0x00403fcc in __gnu_cxx::__alloc_traits<std::allocator<SerializableObject> >::destroy (__a=..., __p=0xb4e398) at d:\mingw-builds\i686-4.8.2-release-posix-dwarf-rt_v3-rev0\mingw32\lib\gcc\i686-w64-mingw32\4.8.2\include\c++\ext\alloc_traits.h:219
    [debug]#8  0x00404a57 in std::vector<SerializableObject, std::allocator<SerializableObject> >::erase (this=0x22fe78, __position=...) at d:\mingw-builds\i686-4.8.2-release-posix-dwarf-rt_v3-rev0\mingw32\lib\gcc\i686-w64-mingw32\4.8.2\include\c++\bits\vector.tcc:140
    [debug]#9  0x00401b1f in main (argc=1, argv=0x3e29b8) at D:\new_opencv_wxWidgets\wxxs-code-112-trunk\samples\Sample5\main.cpp:108
    

    You see, when I delete an SerializableObject in the vector, it finally call some code in wxXmlSerializer, which cause the crash.

    Any ideas to fix this crash issue?

    Thanks.

     
  • Michal Bližňák

    You cannot delete the objects managed by serializer class after invocation of RemoveAll() method manually - the objects were already deleted by the serializer. BTW, why did you use std::vector as a storage for serialized objects? wxXmlSerializer class itself is a persistent storage with rich API for handling of stored objects. IMHO you don't need any other container class to work with stored objects.

     

Log in to post a comment.