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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Destructor problem 1

Status
Not open for further replies.

Fluke026

Programmer
Aug 20, 2002
32
US
Hello,

I am working on a small game and have run into the following problem. Sometimes when I call .free on a certain derived class, I get an AV. Other times I do not. Here's the details.

I have the following classes:

TAI from (TPersistent)
TPlayer from (TAI)
TMonster from (TAI)
TMaze from (TPersistent) that holds the maze, an array of 6 TPlayers, and the TMonster.

I do all manipulation on the classes from public methods. I also have a GUI (holding a TMaze called GameMaze) that displays a grid of the TPlayer stats and a grid of the TMonster stats.

I have two procedures in the form that update the grids by calling public methods in GameMaze: UpdatePlayerGrid and UpdateMonsterGrid. The first time the program calls the updaters (after NewGame1Click) everything is fine. But when it calls the Monster updater after the first move, things go bonkers.

Code:
procedure TMainForm.UpdateMonsterGrid;
var
   Monster: TMonster;
begin
   Monster:= TMonster.Create(0);
   try
      Monster:= GameMaze.GetMonsterInfo;
      with sgMonster do begin
         //fill the grid with the information.
      end; {with}
   finally
      Monster.free;  //this line bombs the second time through
   end; {try/finally}
end;

The UpdatePlayerGrid has very similar code, but it appears to work fine. The main difference is that the player proc uses an instance of TPlayer instead of TMonster and has a loop to process all 6 players, but the TPlayer.free at the end works just fine. Does anyone have any ideas?

Thank you
 
You seem to have assigned to Monster twice in your UpdateMonsterGrid procedure.

First with the constructor, Create(0) and then with GameMaze.GetMonsterInfo.

So, the Monster object created with the constructor will be lost (become a memory leak) and the Monster.free will try to free whatever was assigned to Monster by the GetMonsterInfo function.





 
Interesting. I wondered about that, but the same style of code works just fine in UpdatePlayerGrid, so I didn't test that before. So that might explain why I am getting strange data in the Updated grids (i.e. Valid rooms in the maze are 1 to 30. Last night, I had a Monster in room 84 and a Player in room 27,615 or something like that. In addition, the Monster had 24 HP with a max of 1. Going into the proc, he had 7 of 8 max). Thank you very much).
 
One of the good things about Pascal is the ability to define ranges. So your room type could be defined as

Code:
MinRoom = 1;
MaxRoom = 30;
TRoom = MinRoom..MaxRoom;
If you have range checking enabled in your compiler then it will raise a range check error if there is any attempt to assign a room with an invalid number.

Always use MinRoom and MaxRoom in your code rather than 1 and 30. Then when you alter the number of rooms in your game you will minimise the number of lines of code to change.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top