Advertisement

CDocument contents getting deleted?

Started by February 16, 2001 05:41 PM
1 comment, last by wise_Guy 23 years, 11 months ago
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.state = -1; all_civilians.accel = 0; all_civilians.anim_counter = 0; all_civilians.anim_speed = 0; all_civilians.bonus = 0; all_civilians.curr_frame = 0; all_civilians.decel = 0; all_civilians.direction = 2; all_civilians.explosion = 0; all_civilians.health = 100; all_civilians.height = 24; all_civilians.max_health = 100; all_civilians.shot_counter = 0; all_civilians.xv = 0; all_civilians.yv = 0; all_enemies.state = -1; all_powerups.state = -1; all_powerups.accel = 0; all_powerups.anim_counter = 0; all_powerups.anim_speed = 0; all_powerups.curr_frame = 0; all_powerups.decel = 0; all_powerups.direction = 2; all_powerups.explosion = 0; all_powerups.health = 100; all_powerups.height = 32; all_powerups.max_health = 100; all_powerups.shot_counter = 0; all_powerups.xv = 0; all_powerups.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_width * map_height ; i++) { base_layer.tile = 0; if (i>=((map_height-1) * map_width)) base_layer.tile = 2; fringe_layer.tile = 0; fringe_layer.anim_counter = 0; fringe_layer.bonus = 0; fringe_layer.explosion = 0; fringe_layer.health = -1; fringe_layer.max_health = -1; fringe_layer.owner = 0; fringe_layer.shot_counter = 0; } for (i=map_width*(map_height-2) ; i<map_width*(map_height-1) ; i++) { fringe_layer.tile = 3; } } wow thanks for reading! If you have any suggestions, i would love to hear them! </i>
Ahh (expletive) sorry for posting with i as a looping variable. Mea culpe. But still... it is a vexing problem.
Advertisement
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.

This topic is closed to new replies.

Advertisement