c++ - std::bad_alloc on boost::archive::binary_iarchive -


i have above method serialize generic class.

std::unique_ptr<message> serializer::deserialize(char* data , int size) {     std::stringstream ss(std::ios_base::in |                            std::ios_base::out |                            std::ios_base::binary);     ss.write( data, size );     message *in;      boost::archive::binary_iarchive ia(ss, boost::archive::no_header);     ia >> in;     return  std::unique_ptr<message> (in); } 

i get

 terminate called after throwing instance of 'std::bad_alloc'   what():  std::bad_alloc 

when try call deserialize on char* sure sent integrally. class try deserialize can found here:

class discoverymessage : public message { public:    discoverymessage() {}    discoverymessage(int tcpport);    virtual ~discoverymessage(){}    friend class boost::serialization::access;  private:   template <class archive>   void serialize(archive &ar, const unsigned int ) {     // invoke serialization of base class (message)     ar & boost::serialization::base_object<message>(*this);     ar & mtcpreceiveport;     ar & mipaddr;   }    int mtcpreceiveport;   std::string mipaddr; }; 

make sure deserialize same type serialized.

specifically see mixing in unique_ptr , derived classes. e.g. cannot use above code deserialize discoverymessage* or boost::shared_ptr<message> etc.

simple demo:

live on coliru

#include <boost/serialization/serialization.hpp> #include <boost/serialization/base_object.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/serialization/export.hpp>  class discoverymessage; struct message {     virtual ~message() {}      virtual void do_print(std::ostream& os) const { os << "(base)"; }      friend std::ostream& operator<<(std::ostream& os, message const& m) {         m.do_print(os);         return os;     }      template <class archive> void serialize(archive &ar, const unsigned int) {         ar.template register_type<discoverymessage>();     } };  class discoverymessage : public message {   public:     discoverymessage() {}      discoverymessage(int tcpport) : mtcpreceiveport(tcpport) {}     virtual void do_print(std::ostream& os) const {         os << "{" << mipaddr << ":" << mtcpreceiveport << "}";     }    private:     friend class boost::serialization::access;     template <class archive> void serialize(archive &ar, const unsigned int) {         // invoke serialization of base class (message)         ar & boost::serialization::base_object<message>(*this);         ar & mtcpreceiveport;         ar & mipaddr;     }      int mtcpreceiveport;     std::string mipaddr = "hello"; };  boost_class_export(message) boost_class_export(discoverymessage)  #include <sstream> #include <iostream>  int main() {     std::stringstream ss;     {         boost::archive::text_oarchive oa(ss);         message * p = new discoverymessage(42);         oa << p;     }      {         boost::archive::text_iarchive ia(ss);         message * p;         ia >> p;         std::cout << *p << "\n";     } } 

prints

{hello:42} 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -