• Advertisement

Archived

This topic is now archived and is closed to further replies.

CDocument contents getting deleted?

This topic is 6215 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 all! I have just encountered a bugger of a problem with my MFC code. I initialise my map data in the document constructor, but when I access this data laters, the values have been overwritten with rubbish (6456988, -345879345, 34583496, etc). Strangely, this only happens for the objects (those initialised during the first loop). The tile layers are initialised perfectly. My code is listed below.... CLevelEditorDoc::CLevelEditorDoc() { int i; // initialise all the variables this->all_buildings = new building[MAX_OBJECTS]; this->all_civilians = new civilian[MAX_OBJECTS]; this->all_enemies = new enemy[MAX_OBJECTS]; this->all_powerups = new powerup[MAX_OBJECTS]; for (i=0 ; i.state = -1; all_civilians[i].state = -1; all_civilians[i].accel = 0; all_civilians[i].anim_counter = 0; all_civilians[i].anim_speed = 0; all_civilians[i].bonus = 0; all_civilians[i].curr_frame = 0; all_civilians[i].decel = 0; all_civilians[i].direction = 2; all_civilians[i].explosion = 0; all_civilians[i].health = 100; all_civilians[i].height = 24; all_civilians[i].max_health = 100; all_civilians[i].shot_counter = 0; all_civilians[i].xv = 0; all_civilians[i].yv = 0; all_enemies[i].state = -1; all_powerups[i].state = -1; all_powerups[i].accel = 0; all_powerups[i].anim_counter = 0; all_powerups[i].anim_speed = 0; all_powerups[i].curr_frame = 0; all_powerups[i].decel = 0; all_powerups[i].direction = 2; all_powerups[i].explosion = 0; all_powerups[i].health = 100; all_powerups[i].height = 32; all_powerups[i].max_health = 100; all_powerups[i].shot_counter = 0; all_powerups[i].xv = 0; all_powerups[i].yv = 0; } map_width = 60; map_height = 20; player_start.x = 50; player_start.y = 200; // and the tile layers this->base_layer = new base_tile[map_width * map_height]; this->fringe_layer = new fringe_tile[map_width * map_height]; for (i=0 ; i=((map_height-1) * map_width)) base_layer[i].tile = 2; fringe_layer[i].tile = 0; fringe_layer[i].anim_counter = 0; fringe_layer[i].bonus = 0; fringe_layer[i].explosion = 0; fringe_layer[i].health = -1; fringe_layer[i].max_health = -1; fringe_layer[i].owner = 0; fringe_layer[i].shot_counter = 0; } for (i=map_width*(map_height-2) ; i

Share this post


Link to post
Share on other sites
Advertisement
Ahh (expletive) sorry for posting with i as a looping variable. Mea culpe. But still... it is a vexing problem.

Share this post


Link to post
Share on other sites
CDocument isn''t created from the constructor. If you''ll notice, the constructor is protected. It''s created dynamically from a CRuntimeClass that''s returned from CObject''s "GetRuntimeClass" method. Confusing enough for you? =)

What you need to do is make sure that you move all of the constructor code you have into the function CDocument::OnNewDocument, and all of the destructor code into CDocument::DeleteContents. I don''t know if you have an SDI or MDI application, but you might want to look at the other members too (OnCloseDocument, OnOpenDocument). Hope that helps.

Share this post


Link to post
Share on other sites

  • Advertisement