I'm now working on a game using SFML and I can't remove game states from a "states" std::stack, which causes many problems with loading files and other stuff. What am I doing wrong? debugger said: #0 0x6e1932ed __gnu_cxx::new_allocator::construct(this=0x96d2e4, __p=0xfeeefeee, __val=...) on the call stack (segment fault)
My Code:
Game.cpp:
#include "Game.hpp"
Game::Game()
:   WinWidth(1920)
,   WinHeight(1080)
{
    this->initWindow(); // making a window
    this->initStates();
}
Game::~Game()
{
    delete view;
    delete window;
    std::cout << "[DEBUG]: deleted view\n";
    std::cout << "[DEBUG]: deleted window\n";
    while (!this->states.empty())
    {
        EndState();
    }
    std::cout << "[DEBUG]: Ended the Game\n";
}
void Game::initStates()
{
    this->states.push(new MainMenu(this->window, &this->states, &this->keys, this->view)); // Main Menu
}
void Game::Run()
{
    while (this->window->isOpen())
    {
        this->CheckQuitRequest();
        this->Update();
        this->ClearWindow();
        this->DrawThings();
        this->WindowDisplay();
    }
}
void Game::Update()
{
    this->UpdateDt();
    if (!this->states.empty())
    {
        if (this->states.top()->isQuitting())
        {
           EndState();
            if (this->states.empty())
            {
                EndApp();
            }
        }
    }
    else
    {
        EndApp();
    }
}
void Game::EndState() // crashing with -1073741819 (0xC0000005)
{
    delete states.top();
    this->states.pop();
    std::cout << "[DEBUG]: deleted state\n";
}
void Game::EndApp()
{
    this->window->close();
    exit(0);
    std::cout << "[DEBUG]: closed window\n";
}
Game.hpp
#ifndef GAME_HPP_INCLUDED
#define GAME_HPP_INCLUDED
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
#include <map>
#include <stack>
#include <vector>
#include <cstdlib>
#include <string>
#include <fstream>
#include "States.hpp"
#include "Things.hpp"
#include "ActualGame.hpp"
#include "MainMenu.hpp"
class Game
{
public:
    Game();
    ~Game();
    void initWindow();
    void initStates();
    void Run();
    void UpdateDt();
    void Update();
    void CheckEvents();
    void CheckQuitRequest();
    void ClearWindow();
    void DrawThings();
    void WindowDisplay();
    void EndState();
    void EndApp();
private:
    const int WinWidth;
    const int WinHeight;
    sf::RenderWindow *window;
    sf::View *view;
    sf::Clock dtClock;
    float dt;
    sf::Event event;
    std::map<std::string, int> keys;
    std::stack<States*> states; // <= this stack is the problem
};
#endif // GAME_HPP_INCLUDED
I'm a beginner and this type od coding is new for me, so I'm learning from Suraj Sharma's SFML RPG series, and this operation works in his vids, but my program breaks. I checked it few times and I haven't found any difference. I would be really grateful for help
User contributions licensed under CC BY-SA 3.0