Skip to main content
gamedev.net
🔒 Locked

crashing problem

Started by daniel_i_l Jul 29, 2006 at 4:47 PM 3 replies 650+ views
Original Post
daniel_i_l
daniel_i_l
I have the following function that loads a level:

void InitLevel()
{
   
   int size;
 for(unsigned int i = 0; i < Flock.size(); i++)
     {
      delete Flock;
     }
 Flock.clear();
 
 for(unsigned int i = 0; i < Sheep.size(); i++)
     {
      delete Sheep;
     }
 Sheep.clear();
 
 size = vGroups.size();
 for(unsigned int i = 0; i < size; i++)
     {
      delete vGroups;
     }
 vGroups.clear();
 
 size = vEnemys.size();
 for(unsigned int i = 0; i < size; i++)
     {
      delete vEnemys;
     }
 vEnemys.clear();

  for(int i = 0; i < NumberInFlock; i++)
     {
      CVehicle* v = new CVehicle(Vector2D(35 - i*3 - 3,50));
      Flock.push_back(v);
      //delete v;
     }

    
    //double offsetX=-3;       //Box
    //double offsetY= 4.5;
   
    for(unsigned int i = 1; i < NumberInFlock; i++)
     {
      Flock->CreateObstacles();
      Flock->GetMyAgents(Flock);
      Flock->GetWorldAgents(Flock);
      Flock->SetMaxSpeed(5);
      Flock->Steering()->SetBoxWeight(2);
      Flock->SetGroupId(0);
      Flock->GetHeightMap(g_HeightMap);
     }
       
    for(int i = 0; i < NumberOfSheep; i++)
     {
      CVehicle* v = new CVehicle(Vector2D(65 -2*i,80));
      Sheep.push_back(v);
     }
   
    for(unsigned int i = 0; i < NumberOfSheep; i++)
     {
      Sheep->CreateObstacles();
      Sheep->GetMyAgents(Sheep);
      Sheep->GetWorldAgents(Sheep);
      Sheep->SetMaxSpeed(2);
      Sheep->Steering()->SetBoxWeight(2);
      Sheep->GetHeightMap(g_HeightMap);
     }
   ///////////////////////////////  
   //Set Up The Different Groups
   ///////////////////////////////
   
  // Flock[0]->SetLeader(Flock[0]);
   Flock[0]->CreateObstacles(); 
   Flock[0]->GetWorldAgents(Sheep);
   //Leader->Steering()->PursuitOn(Sheep[0]);
   //Flock[0]->Steering()->ArriveOn();
   Flock[0]->SetMaxSpeed(5);
   Flock[0]->Steering()->SetBoxWeight(1.5);
   Flock[0]->GetHeightMap(g_HeightMap);
   //Flock[0]->SetGroupId(0);
  // Flock[0]->MakeLeader();
   //vGroupHasLeader.push_back(true);
   
   CGroup* g = new CGroup();
   vGroups.push_back(g);
   vGroups[0]->PutVehiclesInGroup(Flock);
   vGroups[0]->PutTarget(Vector2D(20,100));
   vGroups[0]->GetEnemies(Sheep);
   
   vector<CVehicle*>  Units;
   
   Units.assign(Sheep.begin(), Sheep.begin() + 6);
   CEnemy* e = new CEnemy(SitAndWait);
   vEnemys.push_back(e);
   vEnemys[0]->PutVehiclesInGroup(Units);
   vEnemys[0]->GetPlayers(Flock);
   vEnemys[0]->GetHome(Vector2D(50, 50) );
   vEnemys[0]->GetTravelFormation(Row);
   vEnemys[0]->GetAttackFormation(Row);
   
   
   Units.assign(Sheep.begin() + 6, Sheep.end());
   CEnemy* e1 = new CEnemy(Guard);
   vEnemys.push_back(e1);
   vEnemys[1]->PutVehiclesInGroup(Units);
   vEnemys[1]->GetPlayers(Flock);
   vEnemys[1]->GetObstacle(&vObstacles[vObstacles.size()-1]);
   vEnemys[1]->GetBuilding(vBuildings[vBuildings.size()-1]);
   //vEnemys[1]->GetHome(Vector2D(50, 50) );
   vEnemys[1]->GetTravelFormation(Row);
   vEnemys[1]->GetAttackFormation(Row);
   
   //vEnemys[0]->GetObstacle(&vObstacles[0]);
   //vEnemys[0]->PutTarget(Vector2D(100,70));
   
   for(unsigned int i = 0; i < NumberInFlock; i++)
     {
      Flock->GetMoreWorldAgents(Sheep);
     }
   for(unsigned int i = 0; i < NumberOfSheep; i++)
     {
      Sheep->GetMoreWorldAgents(Flock);
     }
   Gun.GetEnemies(Sheep);
   Gun.GetPlayers(Flock); 
   Gun.GetBuildings(vBuildings);
   Gun.GetHeightMap(g_HeightMap);
   
 right_up = true;
 Exit = false;
}

When I run it once it's fine but then when I run it again it crashes. Why? Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Erzengeldeslichtes
Erzengeldeslichtes
I recommend using a debugger to find out exactly where in that function you crash, then figure out why it crashes there. I suspect it's a problem in your deleting code, but running through a debugger will tell you.
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
Zahlman
Zahlman
It looks to me like you're doing a *lot* of *very* strange and messy things. I would create a Level class, and have this work happen in the constructor (and cleanup of the containers in the destructor), so that you know the containers are empty to start with. The "group" class seems rather oddly defined, too; consider separating out the "leader" out from the vector of the "followers" (and why would the individuals need a "group ID" anyway?). Also, I don't see any reason for making the containers store pointers, except in the case of the temporary Units vector; you're not sharing objects between containers (except that Units aliases some subsets of the Sheep), and there doesn't seem to be any polymorphism going on (everything is just a Vehicle, rather than some derived class thereof - wait - hold on, Sheep are Vehicles?). Also, you have functions called "Get(something)" that don't actually appear to be returning members (accessors), but instead are setting things up (mutators). Also, lots of stuff is redundant, and things are repeated but done in inconsistent ways. You also seem to have huge amounts of global variables. Oh, and don't use 'x[x.size()-1]' to get at the last element of a vector; use 'x.back()'.
daniel_i_l
daniel_i_l
I know that it's messy but it's the end of a long project and there're other things I would rather be working on - I just want to get this to work.
i found out that the problem is with the first part were I delete all the units before assigning them with new values. Whats wrong with that?
Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
iMalc
iMalc
Quote:
Original post by daniel_i_l
I know that it's messy but it's the end of a long project and there're other things I would rather be working on - I just want to get this to work.
i found out that the problem is with the first part were I delete all the units before assigning them with new values. Whats wrong with that?
Thanks.
From what I cna tell, nothing is essentially wrong with it. You obviously have gotten some stray random pointer value in there or somthing, which is causing the problem.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.