[serialization - newbie] serializing/deserializing singleton class objects
Hi, This is query was posted as part of different question. Doing a fresh post with a new topic as earlier one didn't fetch any response - assuming it may got lost in the original context. I'm trying to serialize/deserialize singleton class objects. My code is similar to following for save/restore: void save(boost::archive::binary_oarchive &archive) { MyData *data = MyData::instance(); if (data) archive << data; } void restore(boost::archive::binary_iarchive &archive) { MyData::cleanup(); archive >> (MyData *) MyData::instance(); } My save function works - stores data on disk. But the restore function doesn't compile. I get error like following: error: no match for ‘operator>>’ in ‘archive >> (MyData *) MyData::instance()’ /boost/1_35_0/include/boost/archive/detail/interface_iarchive.hpp:62: note: candidates are: Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = MyData*, Archive = boost::archive::binary_iarchive] If I change the 2nd line of restore function in following way, the error message almost remains same: MyData *inst = MyData::instance(); archive >> (MyData *) inst; Please guide me what I'm doing wrongly. Regards, ~ Soumen -- View this message in context: http://www.nabble.com/-serialization---newbie--serializing-deserializing-sin... Sent from the Boost - Users mailing list archive at Nabble.com.
There're some vector of raw pointer to const objects in MyData. Removing the const-ness from those pointer resolved the compilation issue. However, it seems some smart pointer inside serialization library code is freeing those memory and that's causing a crash later on. Is it mandatory, know it's advisable, that I need to use smart pointer instead of raw pointer in that vector here? Regards, ~ Soumen -- View this message in context: http://www.nabble.com/-serialization---newbie--serializing-deserializing-sin... Sent from the Boost - Users mailing list archive at Nabble.com.
Not totally sure what you problem is.
Have you considered tracking ? (
There're some vector of raw pointer to const objects in MyData. Removing the const-ness from those pointer resolved the compilation issue. However, it seems some smart pointer inside serialization library code is freeing those memory and that's causing a crash later on.
Is it mandatory, know it's advisable, that I need to use smart pointer instead of raw pointer in that vector here?
Regards, ~ Soumen
-- View this message in context: http://www.nabble.com/-serialization---newbie--serializing-deserializing-sin... Sent from the Boost - Users mailing list archive at Nabble.com.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
After I did some exception handling, it turned out that during serialization some unregistered void cast was happening and that's causing stream error. Probably debugger was pointing to wrong frame :( After fixing those exception, crash is resolved. But I've new problem - serialize doesn't write all data. And it seems the problem is because of some ABC. Let me explain it here: I've following class structure A <--+-- B | +-- C A is ABC and has no data member. B and C has data member. Some class D has vector as data member. And my serialization code is something like following: D *dObj = D::instance(); oparchive << (const D&)(*dObj); De-serialization code is something like following: D::cleanup(); D *dObj = D::instance(); iparchive >> (*dObj); I declare A as abstract through BOOST_IS_ABSTRACT. However, I also write an empty serialize() for A. Also, B and C serialization() uses base_object (*this). And B and C are exported too. Seems to me, the empty A::serialize() is causing the problem. But if I remove it, the code doesn't compile unless I remove base_object (*this) from B::serialize() and C::serialize(). I've verified that number of A objects in D after deserialization is same as the number before serialization. But those B or C objects (deserialised through A*) seems empty. Regards, ~ Soumen -- View this message in context: http://www.nabble.com/-serialization---newbie--serializing-deserializing-sin... Sent from the Boost - Users mailing list archive at Nabble.com.
Soumen wrote: [snip]
D::cleanup(); D *dObj = D::instance(); iparchive >> (*dObj);
[snip] I am also trying to do serialization of singletons (see post regarding serialization and error category). Have you considered using the stuff available for "non-default constructors" (see doc) for loading? E.g., template<class Archive> inline void load_construct_data( Archive & ar, D * dObj, const unsigned int file_version ){ dObj = D::instance(); } Cheers, Rutger
Since after restoration of D, I've same size of vector member of D, I assume that's not the issue. Correct me if my assumption is wrong. Now, my problem is those B and C objects are empty. And it seems to me while serializing itself, those didn't get stored on disk - the unix strings command reveals that none of string objects in B or C type objects are there on the saved archive. Regards, ~ Soumen -- View this message in context: http://www.nabble.com/-serialization---newbie--serializing-deserializing-sin... Sent from the Boost - Users mailing list archive at Nabble.com.
Soumen wrote:
Since after restoration of D, I've same size of vector member of D, I assume that's not the issue. Correct me if my assumption is wrong. Now, my problem is those B and C objects are empty. And it seems to me while serializing itself, those didn't get stored on disk - the unix strings command reveals that none of string objects in B or C type objects are there on the saved archive.
Have you tried serializing to an XML archive to be able to see what's going on? Have you verified "Note that the implemenation of this functionality requires that the BOOST_CLASS_EXPORT macro appear after and the inclusion of any archive class headers for which code is to be instantiated."? Kind regards, Rutger
Have you verified "Note that the implemenation of this functionality requires that the BOOST_CLASS_EXPORT macro appear after and the inclusion of any archive class headers for which code is to be instantiated."? ---- Though this is ensured in my code, could you point me to this in the doc? BTW, I just put some cout in serialize() function of A, B, C and D to verify if serialize is getting called or not for all of these classes. And strangely I see, serialize() is getting called only for D. Not for A, B or C. I'm not getting any exception either. Assuming member of type vector in D is data_, in my D::serialize() I just do ar & data_; I'm already including vector.hpp. Am I missing something? Regards, ~ Soumen -- View this message in context: http://www.nabble.com/-serialization---newbie--serializing-deserializing-sin... Sent from the Boost - Users mailing list archive at Nabble.com.
Soumen wrote:
Though this is ensured in my code, could you point me to this in the doc?
BTW, I just put some cout in serialize() function of A, B, C and D to verify if serialize is getting called or not for all of these classes. And strangely I see, serialize() is getting called only for D. Not for A, B or C. I'm not getting any exception either. Assuming member of type vector in D is data_, in my D::serialize() I just do
ar & data_;
I'm already including vector.hpp. Am I missing something?
Exporting is discussed at Reference/Serializable Concept/Export Reference/Special Considerations/Exporting Class Serialization the note on order of includes is in the second one. There's more information on the type registry at Other Classes/extended_type_info Something like the php-docs have with comments and stuff would be nice for serialization. Do you have a (slimmed down) example of your code? Cheers, Rutger
I think there is some special considerations to be taken when serializing
base classes if you haven't already taken that into account. I also would
suggest you post some minimalistic example. Take a look at this:
http://www.boost.org/doc/libs/1_39_0/libs/serialization/doc/serialization.ht...
http://www.boost.org/doc/libs/1_39_0/libs/serialization/doc/serialization.ht...
Cheers
Rune
On Thu, Aug 13, 2009 at 8:51 AM, Rutger ter Borg
Soumen wrote:
Though this is ensured in my code, could you point me to this in the doc?
BTW, I just put some cout in serialize() function of A, B, C and D to verify if serialize is getting called or not for all of these classes. And strangely I see, serialize() is getting called only for D. Not for A, B or C. I'm not getting any exception either. Assuming member of type vector in D is data_, in my D::serialize() I just do
ar & data_;
I'm already including vector.hpp. Am I missing something?
Exporting is discussed at
Reference/Serializable Concept/Export Reference/Special Considerations/Exporting Class Serialization
the note on order of includes is in the second one. There's more information on the type registry at
Other Classes/extended_type_info
Something like the php-docs have with comments and stuff would be nice for serialization.
Do you have a (slimmed down) example of your code?
Cheers,
Rutger
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
The general problem is resolved - there's some ugly typo in my code :-(( Now on some simple test cases, save/restore for my data structure works great. Need to do more testing. Will seek help from forum when stuck again. Regards, ~ Soumen -- View this message in context: http://www.nabble.com/-serialization---newbie--serializing-deserializing-sin... Sent from the Boost - Users mailing list archive at Nabble.com.
participants (3)
-
Rune Lund Olesen
-
Rutger ter Borg
-
Soumen