Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Using boost serialization to send a vector.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Fezziiwig   Members   -  Reputation: 140

Like
0Likes
Like

Posted 02 November 2012 - 05:56 PM

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


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

Thanks,
Rowan.

Sponsor:

#2 wood_brian   Banned   -  Reputation: 197

Like
0Likes
Like

Posted 03 November 2012 - 01:29 PM

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.

#3 hplus0603   Moderators   -  Reputation: 5723

Like
0Likes
Like

Posted 03 November 2012 - 05:50 PM

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, 03 November 2012 - 05:51 PM.

enum Bool { True, False, FileNotFound };




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS