Jump to content
  • Advertisement
Sign in to follow this  
Alpha Nox

Serialization weird problems

This topic is 4253 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

Hi, I'm currently making a simple map editor for my game Phoenix Online and I'm having problems when trying to serialize the text npcs say in the game in the map file, other things such as sprites information and etc works just fine but I can't seem to work this out, since the debugger isn't helping much. When I create the npctalk object it works just fine, and it also saves normally the problem occurs when I try to load the map file. I only get An error ocurred in Phoenix Online map editor and it has to be closed WinXP message. The debugger image is this one: http://i112.photobucket.com/albums/n174/alphanox/debugimage.jpg If you want anything more of the code,please just say and I will post it here. I'd be very grateful if someone could help me with this. Thanks in advance, Victor Freire How do I put the code in a box? The code is a bit big so I only put the relevant parts below(the class im trying to save and open, the header of the file and the open and save functions) class build_mapnpctalk { public: build_mapnpctalk(); string name; int frames; string chatlines[17][10]; }; build_mapnpctalk::build_mapnpctalk() { name = "undefined"; frames = 0; for(int loop = 0;loop < 15;loop++) { for(int loop2 = 0;loop2 < 8;loop2++) { chatlines[loop][loop2] = "nothing"; } } } class build_map { public: build_map(); string background; vector<build_map_piece> map_pieces; vector<build_map_bigpiece> map_bigpieces; vector<build_mapnpc> map_npcs; vector<build_maplighting> map_lighting; vector<build_mapcollisionbox> map_colboxes; vector<build_mapusebox> map_useboxes; vector<build_mapwalkbox> map_walkboxes; vector<build_mapgroundmatbox> map_gmatboxes; vector<build_mapnpctalk> map_npctalks; vector<build_maptextline> map_textlines; string mapnorth; string mapsouth; string mapwest; string mapeast; string bgmusic; }currentmap; build_map::build_map() { background = "grass"; bgmusic = "none"; } struct mapfileheader { long newobj_id; string background; string bgmusic; string mapnorth; string mapsouth; string mapwest; string mapeast; bool map_pieces_empty; bool map_bigpieces_empty; bool map_npcs_empty; bool map_lighting_empty; bool map_colboxes_empty; bool map_useboxes_empty; bool map_walkboxes_empty; bool map_gmatboxes_empty; bool map_npctalks_empty; bool map_textlines_empty; int map_pieces_amount; int map_bigpieces_amount; int map_npcs_amount; int map_lighting_amount; int map_colboxes_amount; int map_useboxes_amount; int map_walkboxes_amount; int map_gmatboxes_amount; int map_npctalks_amount; int map_textlines_amount; }; vector<build_map_piece>::iterator map_piecesITR; vector<build_map_bigpiece>::iterator map_bigpiecesITR; vector<build_mapnpc>::iterator map_npcsITR; vector<build_maplighting>::iterator map_lightingITR; vector<build_mapusebox>::iterator map_useboxesITR; vector<build_mapcollisionbox>::iterator map_colboxesITR; vector<build_mapwalkbox>::iterator map_walkboxesITR; vector<build_mapgroundmatbox>::iterator map_gmatboxesITR; vector<build_mapnpctalk>::iterator map_npctalksITR; vector<build_maptextline>::iterator map_textlinesITR; void OpenMap(string filename) { string filepath = "maps/" + filename + ".pm"; mapin.open(filepath.c_str()); if(!mapin.is_open()) { cerr << "COULDNT OPEN '" << filename << "' for reading!!!\n"; system("pause"); return; } currentmapname = filename; currentmap.map_pieces.clear(); currentmap.map_bigpieces.clear(); currentmap.map_npcs.clear(); currentmap.map_lighting.clear(); currentmap.map_colboxes.clear(); currentmap.map_useboxes.clear(); currentmap.map_walkboxes.clear(); currentmap.map_gmatboxes.clear(); currentmap.map_npctalks.clear(); currentmap.map_textlines.clear(); mapin.read((char*)&curmapheader, sizeof(curmapheader)); currentmap.background = curmapheader.background; currentmap.bgmusic = curmapheader.bgmusic; currentmap.mapnorth = curmapheader.mapnorth; currentmap.mapsouth = curmapheader.mapsouth; currentmap.mapwest = curmapheader.mapwest; currentmap.mapeast = curmapheader.mapeast; if(curmapheader.map_pieces_amount != 0) { for(int loop = 1;loop <= curmapheader.map_pieces_amount;loop++) { build_map_piece b; mapin.read((char*)&b, sizeof(b)); currentmap.map_pieces.push_back(b); } } if(curmapheader.map_bigpieces_amount != 0) { for(int loop = 1;loop <= curmapheader.map_bigpieces_amount;loop++) { build_map_bigpiece b; mapin.read((char*)&b, sizeof(b)); currentmap.map_bigpieces.push_back(b); } } if(curmapheader.map_npcs_amount != 0) { for(int loop = 1;loop <= curmapheader.map_npcs_amount;loop++) { build_mapnpc b; mapin.read((char*)&b, sizeof(b)); currentmap.map_npcs.push_back(b); } } if(curmapheader.map_lighting_amount != 0) { for(int loop = 1;loop <= curmapheader.map_lighting_amount;loop++) { build_maplighting b; mapin.read((char*)&b, sizeof(b)); currentmap.map_lighting.push_back(b); } } if(curmapheader.map_useboxes_amount != 0) { for(int loop = 1;loop <= curmapheader.map_useboxes_amount;loop++) { build_mapusebox b; mapin.read((char*)&b, sizeof(b)); currentmap.map_useboxes.push_back(b); } } if(curmapheader.map_colboxes_amount != 0) { for(int loop = 1;loop <= curmapheader.map_colboxes_amount;loop++) { build_mapcollisionbox b; mapin.read((char*)&b, sizeof(b)); currentmap.map_colboxes.push_back(b); } } if(curmapheader.map_walkboxes_amount != 0) { for(int loop = 1;loop <= curmapheader.map_walkboxes_amount;loop++) { build_mapwalkbox b; mapin.read((char*)&b, sizeof(b)); currentmap.map_walkboxes.push_back(b); } } if(curmapheader.map_gmatboxes_amount != 0) { for(int loop = 1;loop <= curmapheader.map_gmatboxes_amount;loop++) { build_mapgroundmatbox b; mapin.read((char*)&b, sizeof(b)); currentmap.map_gmatboxes.push_back(b); } } if(curmapheader.map_npctalks_amount != 0) { for(int loop = 1;loop <= curmapheader.map_npctalks_amount;loop++) { build_mapnpctalk b; mapin.read((char*)&b, sizeof(b)); currentmap.map_npctalks.push_back(b); } } if(curmapheader.map_textlines_amount != 0) { for(int loop = 1;loop <= curmapheader.map_textlines_amount;loop++) { build_maptextline b; mapin.read((char*)&b, sizeof(b)); currentmap.map_textlines.push_back(b); } } mapin.close(); cout << "MAP LOADED!!!\n"; } void SaveMap(string filename) { string filepath = "maps/" + filename + ".pm"; mapout.open(filepath.c_str()); if(!mapout.is_open()) { cerr << "COULDNT OPEN '" << filename << "' for writing!!!\n"; system("pause"); return; } mapfileheader curmapheader; curmapheader.map_pieces_empty = currentmap.map_pieces.empty(); curmapheader.map_bigpieces_empty = currentmap.map_bigpieces.empty(); curmapheader.map_npcs_empty = currentmap.map_npcs.empty(); curmapheader.map_lighting_empty = currentmap.map_lighting.empty(); curmapheader.map_colboxes_empty = currentmap.map_colboxes.empty(); curmapheader.map_useboxes_empty = currentmap.map_useboxes.empty(); curmapheader.map_walkboxes_empty = currentmap.map_walkboxes.empty(); curmapheader.map_gmatboxes_empty = currentmap.map_gmatboxes.empty(); curmapheader.map_npctalks_empty = currentmap.map_npctalks.empty(); curmapheader.map_textlines_empty = currentmap.map_textlines.empty(); curmapheader.map_pieces_amount = (int)currentmap.map_pieces.size(); curmapheader.map_bigpieces_amount = (int)currentmap.map_bigpieces.size(); curmapheader.map_npcs_amount = (int)currentmap.map_npcs.size(); curmapheader.map_lighting_amount = (int)currentmap.map_lighting.size(); curmapheader.map_colboxes_amount = (int)currentmap.map_colboxes.size(); curmapheader.map_useboxes_amount = (int)currentmap.map_useboxes.size(); curmapheader.map_walkboxes_amount = (int)currentmap.map_walkboxes.size(); curmapheader.map_gmatboxes_amount = (int)currentmap.map_gmatboxes.size(); curmapheader.map_npctalks_amount = (int)currentmap.map_npctalks.size(); curmapheader.map_textlines_amount = (int)currentmap.map_textlines.size(); curmapheader.background = currentmap.background; curmapheader.bgmusic = currentmap.bgmusic; curmapheader.mapnorth = currentmap.mapnorth; curmapheader.mapsouth = currentmap.mapsouth; curmapheader.mapwest = currentmap.mapwest; curmapheader.mapeast = currentmap.mapeast; mapout.write((char*) (&curmapheader),sizeof(mapfileheader)); if(!currentmap.map_pieces.empty()) { for(map_piecesITR = currentmap.map_pieces.begin();map_piecesITR != currentmap.map_pieces.end();map_piecesITR++) { mapout.write((char*) (&*map_piecesITR),sizeof(build_map_piece)); } } if(!currentmap.map_bigpieces.empty()) { for(map_bigpiecesITR = currentmap.map_bigpieces.begin();map_bigpiecesITR != currentmap.map_bigpieces.end();map_bigpiecesITR++) { mapout.write((char*) (&*map_bigpiecesITR),sizeof(build_map_bigpiece)); } } if(!currentmap.map_npcs.empty()) { for(map_npcsITR = currentmap.map_npcs.begin();map_npcsITR != currentmap.map_npcs.end();map_npcsITR++) { mapout.write((char*) (&*map_npcsITR),sizeof(build_mapnpc)); } } if(!currentmap.map_lighting.empty()) { for(map_lightingITR = currentmap.map_lighting.begin();map_lightingITR != currentmap.map_lighting.end();map_lightingITR++) { mapout.write((char*) (&*map_lightingITR),sizeof(build_maplighting)); } } if(!currentmap.map_useboxes.empty()) { for(map_useboxesITR = currentmap.map_useboxes.begin();map_useboxesITR != currentmap.map_useboxes.end();map_useboxesITR++) { mapout.write((char*) (&*map_useboxesITR),sizeof(build_mapusebox)); } } if(!currentmap.map_colboxes.empty()) { for(map_colboxesITR = currentmap.map_colboxes.begin();map_colboxesITR != currentmap.map_colboxes.end();map_colboxesITR++) { mapout.write((char*) (&*map_colboxesITR),sizeof(build_mapcollisionbox)); } } if(!currentmap.map_walkboxes.empty()) { for(map_walkboxesITR = currentmap.map_walkboxes.begin();map_walkboxesITR != currentmap.map_walkboxes.end();map_walkboxesITR++) { mapout.write((char*) (&*map_walkboxesITR),sizeof(build_mapwalkbox)); } } if(!currentmap.map_gmatboxes.empty()) { for(map_gmatboxesITR = currentmap.map_gmatboxes.begin();map_gmatboxesITR != currentmap.map_gmatboxes.end();map_gmatboxesITR++) { mapout.write((char*) (&*map_gmatboxesITR),sizeof(build_mapgroundmatbox)); } } if(!currentmap.map_npctalks.empty()) { for(map_npctalksITR = currentmap.map_npctalks.begin();map_npctalksITR != currentmap.map_npctalks.end();map_npctalksITR++) { mapout.write((char*) (&*map_npctalksITR),sizeof(build_mapnpctalk)); } } if(!currentmap.map_textlines.empty()) { for(map_textlinesITR = currentmap.map_textlines.begin();map_textlinesITR != currentmap.map_textlines.end();map_textlinesITR++) { mapout.write((char*) (&*map_textlinesITR),sizeof(build_maptextline)); } } mapout.close(); }

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 joanusdmentia
Here'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

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!