Hi, I am making a game using SFML. I have implemented a state handling system and am just filling in the bare bones e.g. closing the game and changing states. However I have discovered an issue. When I close the window within the intro/splash state everything is fine. However when I close it from the menu state I get an access violation. The following is the layout of the classes:
Thanks for the replies. I have moved the variables out of protected and into their respective classes as private. Also kbw I think they should be pointers as they change the data for variables that are part of the engine class to determine what state the program is in as well as to close the program down. The program now closes properly without any errors, without deleting the pointers. I then add delete ptr and this causes debug assertion failed to appear and when debugging it is the delete pointer that is causing it.
I see so if I have understood this correctly I need to do something like
object.ChangeState() rather than passing the address of the variable I wish to change? If this is correct then withint the structure of my program I don't see how to do it as the structure is like this:
main: create engine object
engine.cpp set up the environment, such as current state, state list, create the window and handle the main game loop.
states (e.g. intro, menu, credits etc) contain virtual functions that are called by from within engine object.
The system I have in place for changing state and ending the program has me passing the address of these two variables to the current state.
Engine main loop as well as the initialising function (called only once) looks like:
> Do you only delete variables that are created with the new prefix?
Exactly (except that new is not a prefix), and everything that you new must be deleted (otherwise is a leak)
So you must delete each state.
> I need to do something like object.ChangeState() rather than passing the address of the variable I wish to change?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
states[curState]->Initialise(this); //consider using the constructor instead
// int *cState;
// bool *run;
//void cIntro::Input(sf::Window *window)
//*cState = 1;
//*run = false;