Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object Serialization Stream.read()

Status
Not open for further replies.

simoncarter2003

Programmer
Aug 23, 2003
43
0
0
GB
Im using stringstreams to serialize and unserialize an instance of my class to send it over a network. However I've encountered an access violation when I try to pass my unserialized object between functions.

The serialization and unserialization of my objects seems to work correctly, and after the unserialization I can call functions from within the class and it works as it should do. But when I pass this new object to a different function I get the access violation error; note however that this doesn't happen if I don't call the .read() function into the object prior though.

From researching on the Internet, I *think*, that this is because I'm having to use a char* conversion because I'm using the .read() function, and the compiler is therefore putting my object into read only memory, and therefore I get my violation error when writing is attempted to the memory location.

So I have two questions: If my understanding of the error is correct, how can I prevent it being placed in read-only memory. And if I can't how can I copy this object into a different object thats in standard memory.

Code:
Datagram Handler::UnserializeDatagram(std::string strin, int size) {
	std::istringstream strstream(strin);
	Datagram Message;
	strstream.read((char*)&Message, sizeof(Message));
	return Message;
}

My error is: "0xC0000005: Access violation writing location 0x73696854."

Thanks in advance,
Simon
 
Ok, I have actually found out, that I get the access violation error, regardless of what I return. So its not so much an error with passing that unserialized object, but when the function returns (regardless of what it returns), if read has been called, it will crash,
 
> My error is: "0xC0000005: Access violation writing location 0x73696854."
I find it significant that your fault address consists entirely of printable characters.
"sihT"

Since the x86 is little endian, it spells "This" when reversed. Is "This" some text in your input stream?

What exactly is a Datagram structure?

It looks to me like it is say
Code:
struct Datagram {
   char *message;
};
That is, you wrote 4 characters from your stream (This) directly into that pointer, and then tried to dereference that pointer.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top