EAccess Violation in small game

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 6
Reputation: Sam_Surrey is an unknown quantity at this point 
Solved Threads: 0
Sam_Surrey Sam_Surrey is offline Offline
Newbie Poster

EAccess Violation in small game

 
0
  #1
May 13th, 2009
Hi guys, i have been having problems with this piece of code all afternoon, i have never seen this error myself before and cant see how the problem is applicable in my situation from looking though the various forums of the world !

Basically the program is a small game where there are two cars that race each other to a pre defined point on the page, my problem lies in a small piece of code that updates the position of a car.

The error is detailed as: "Debugger exception notice: Project Drag.exe raised exception class EAccess violation. Access violation at adress 00401e13 in module drag.exe. Read of address 00000000".

I have read about null pointers but i dont not completely understand how they work with vectors, any help would be appreciated !

The vector is created in the TMainForm, using
  1. private: // User declarations
  2.  
  3. public: // User declarations
  4. std::vector<RacingCar*> cars ;

This section shows the stopwatch that is set to execute the race itself

void __fastcall TMainForm::StopWatchTimer(TObject *Sender)
{
    TTimer* timer = static_cast<TTimer*>(Sender);
    int dt = timer->Interval / 1000;

    {    // drive the cars
        for(int i = 0; i != cars.size(); ++i)
            cars[i]->Drive(dt);

        int i = 0;
        // update the race track
        Image2->Left += cars[i++]->DistanceTravelled();  /*This line is the one
        causing the error, when commented out rogram works fine.*/

    }

}

While there is no error shown in this section the code is related to it so i thoguht i should show it anyway...

  1. void RacingCar::Drive(unsigned int time)
  2. {
  3. //Determine when the winner has got tot he finish line ands make them stop
  4. if (mileometer <=1200)
  5. {
  6. // work our speed increase
  7. unsigned int speed_inc = Acceleration() * time;
  8.  
  9. // check maximum speed is not exceeded
  10. if(speed + speed_inc > MaximumSpeed())
  11. speed_inc = MaximumSpeed() - speed;
  12.  
  13. // calculate distance travelled
  14. mileometer += (speed + speed_inc) / 2 * time;
  15.  
  16. Oil = random(100);
  17. if (Oil == 50) {
  18. speed = 0;
  19. speed_inc = 0;
  20. }
  21.  
  22. // update speed
  23. speed += speed_inc;
  24.  
  25. // apply drag
  26. if(Drag() > speed)
  27. speed = 0;
  28. else
  29. speed -= Drag();
  30.  
  31. }
  32. else {
  33. speed = 0;
  34. winner = mileometer;
  35. }
  36.  
  37. }


Sorry for the info overload !

Sam
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 53
Reputation: nexocentric is an unknown quantity at this point 
Solved Threads: 4
nexocentric nexocentric is offline Offline
Junior Poster in Training

Re: EAccess Violation in small game

 
0
  #2
May 13th, 2009
Could you post the code for the declaration of RacingCar?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 6
Reputation: Sam_Surrey is an unknown quantity at this point 
Solved Threads: 0
Sam_Surrey Sam_Surrey is offline Offline
Newbie Poster

Re: EAccess Violation in small game

 
0
  #3
May 13th, 2009
  1. RacingCar::RacingCar()
  2. :mileometer(0)
  3. ,speed(0)
  4. ,Oil(0)
  5. {
  6. }

  1. class RacingCar
  2. {
  3. public:
  4. RacingCar();
  5. void Drive(unsigned int time);
  6. unsigned int DistanceTravelled() const;
  7. unsigned int Winner() const;
  8. private:
  9. unsigned int Oil;
  10. unsigned int mileometer;
  11. unsigned int speed;
  12. unsigned int winner;
  13. virtual unsigned int Acceleration() const = 0;
  14. virtual unsigned int MaximumSpeed() const = 0;
  15. virtual unsigned int Drag() const = 0;
  16. } ;

Basically i have a copy of a program that is similar to mine that complies that was given to me by a lecturer but he is external so i havent seen him for 2-3 week so i cant get any help off him :/
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 53
Reputation: nexocentric is an unknown quantity at this point 
Solved Threads: 4
nexocentric nexocentric is offline Offline
Junior Poster in Training

Re: EAccess Violation in small game

 
0
  #4
May 13th, 2009
Image2->Left += cars[i++]->DistanceTravelled();

Is I always supposed to be 1? Looking through the code you posted, it seems that I will always end up being 1. Is there a value in the vector cars at position 1?

One thing you can do to debug this would be to do the following:

cars[i++]->DistanceTravelled();

if you put this on a line by itself and it still crashes, the error occurs during accessing the vector. If it doesn't, then the error occurs when you try to assign a value to Image2->Left.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 6
Reputation: Sam_Surrey is an unknown quantity at this point 
Solved Threads: 0
Sam_Surrey Sam_Surrey is offline Offline
Newbie Poster

Re: EAccess Violation in small game

 
0
  #5
May 13th, 2009
Yeah the error is in accesing the vector, i kind of assumed this was the problem from the start...

To answer the 1st question there is Image3->Left +=cars[i++]->DistanceTravelled() , the i++ basically acts to move on to the next car unless i have completely mis understood the way the program works from my lecturer.... xD

The Accelleration speed etc all come from a seperate unit (e.g. Ferrari.cpp) and since i have the program working in a different place/execution know the concept is fairly sound...
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 6
Reputation: Sam_Surrey is an unknown quantity at this point 
Solved Threads: 0
Sam_Surrey Sam_Surrey is offline Offline
Newbie Poster

Re: EAccess Violation in small game

 
0
  #6
May 14th, 2009
Sorry to bump, its only a small one though xD

Does anyone else have any ideas on this as i am still stumped royally and cant seem to find a way to make it work....

Could it be a problem with my debugger options ?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC