Public Group

# Serialization weird problems

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

## Recommended Posts

##### Share on other sites
"How do I put the code in a box?"

This will tell you how to use code tags. As for your other question, I'm sure you'll find help soon.

Good luck.

##### Share on other sites
Here's the relevant parts of the code causing the crash:

class build_mapnpctalk{public:    build_mapnpctalk();    string name;    int frames;    string chatlines[17][10];};// Code from OpenMap()build_mapnpctalk b;mapin.read((char*)&b, sizeof(b));currentmap.map_npctalks.push_back(b);

You can only read POD (plain old data) types the way you're doing it (and even then it's questionable because of alignment issues). Because of the way std::string works at least one of it's member variables is a pointer, so when you write the data to disk (presumably with a single call to write()) you only write the pointer to disk, not the data it points to. Then later when you read the data back in you similarly read the pointer, but now that address is completely meaningless and will probably give you an access violation when you try to use it.

To find out how to do this properly, start here.

##### Share on other sites
Quote:
 Original post by anothrguitarist"How do I put the code in a box?" This will tell you how to use code tags. As for your other question, I'm sure you'll find help soon.Good luck.

Thanks, I just keep forgetting those tags. =P

Quote:
 Original post by joanusdmentiaHere's the relevant parts of the code causing the crash:*** Source Snippet Removed ***You can only read POD (plain old data) types the way you're doing it (and even then it's questionable because of alignment issues). Because of the way std::string works at least one of it's member variables is a pointer, so when you write the data to disk (presumably with a single call to write()) you only write the pointer to disk, not the data it points to. Then later when you read the data back in you similarly read the pointer, but now that address is completely meaningless and will probably give you an access violation when you try to use it.To find out how to do this properly, start here.

Oh I didn't know that. The debugger was showing a bad_ptr error and I was curious where it was coming from.

Thanks a lot for your help, I've been stuck with this for sometime, I'm going to read that text now.

Thanks again,

Victor Freire

• 16
• 11
• 9
• 49
• 12
• ### Forum Statistics

• Total Topics
631392
• Total Posts
2999738
×