• Advertisement
Sign in to follow this  

Using boost serialization to send a vector.

This topic is 1907 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've run into a real problem with regards to sending serialized vectors over a socket. A simplified example of what I'm doing:

Server:

#include <boost/serialization/vector.hpp>


struct someObject
{
int i;
template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & i;
}
}

struct OutStream
{
std::vector <someObject> someVector;

template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & someVector;
}
}outStream;

// Another part of the code
outStream.someVector.swap(someOtherVector);

// Serialise
std::stringstream archive_stream;
boost::archive::text_oarchive archive(archive_stream);
archive << outStream;


// Send
sendto(ServerSocket, archive_stream.str().c_str(), strlen(archive_stream.str().c_str())...


Client:

#include <boost/serialization/vector.hpp>

struct someObject
{
int i;
template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & i;
}
}

struct InStream
{
std::vector <someObject> someVector;

template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & someVector;
}
}inStream;

// Another part of the code
// Deserialize
archive_stream << temp;
boost::archive::text_iarchive archive(archive_stream);
ZeroMemory(&inStream, sizeof(inStream));
archive >> inStream;

someOtherVector.swap(inStream.someVector);


However when I run this, on the server side, either it doesn't send proberly(lots of blank variables) or it says
Expression:vector iterator not dereferencable[/quote]

If it would help I can upload the whole source code (it's not that large). Any help is much appreciated!

Thanks,
Rowan.

Share this post


Link to post
Share on other sites
Advertisement
I would recommend using a more specific type than int in your marshalling code. The meaning of int can change between platforms.

This line:

ZeroMemory(&inStream, sizeof(inStream));

looks questionable. I didn't see the definition of that function, but maybe you don't need to call that function.

If you are interested in switching to binary serialization, I could be of more help.

Share this post


Link to post
Share on other sites
ZeroMemory() is the same thing as memset() with a 0 argument. You can only use it on POD (plain old data) values. Your InStream struct is not POD, as it contains a std::vector container.
What you probably want is instead:

inStream.someVector.clear();


Or, the idiomatic:

std::vector<someObject>().swap(inStream.someVector);


which is guaranteed to clean up any allocated memory right away, rather than just marking it as "use this next time something goes into this vector."
Edited by hplus0603

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement