Search code examples
c++sfml

Access violation error window.isOpen() SFML(C++)


So I am getting this error: "Uncaught exception occurred on SFML.exe at address 0x5539836A (sfml-window-d-2.dll): 0xC0000005: Read access violation to location 0x0000000C."

main.cpp

#include <iostream>
#include "Game.h"




int main()
{
    //Init game engine

    Game game;
   
    

    //Game loop

    while (game.isRunnig())
    {
        // Event polling
      
        // Update
        game.update();
        // Render
        game.render();
        
        // Draw your Game 
      

    }



    //End of Application

    return 0;
}

Game.h

#pragma once

#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>

/*
    Class that acts as the game engine
    Wrapper Class
*/
class Game
{
private:
    //Variables
    //Window
    sf::RenderWindow* window;
    sf::VideoMode videoMode;
    sf::Event ev;
    
    //Private functions
    void initializeVariable();
    void initWindow();

public:
    // Constructor and Deconstructor
    Game();

    virtual ~Game();
    // Accesors
    const bool isRunnig() const **I am getting the error here**
    {
        return this->window->isOpen();
    };


    
    //  Functions
    void handleEvents();
    void update();
    void render();
};

Game.cpp

#include "Game.h"

void Game::initializeVariable()
{
    this->window = nullptr;
    
}

void Game::initWindow()
{
    this->videoMode.height = 600;
    this->videoMode.height = 800;

    this->window = new sf::RenderWindow(videoMode, "Bioyy 2 ", sf::Style::Titlebar | sf::Style::Close);
}

// Constructor and Deconstructor,
Game::Game()
{
}

Game::~Game()
{
}




void Game::handleEvents()
{
    while (window->pollEvent(this->ev))
    {
        switch (ev.type) {
        case sf::Event::Closed:
            window->close();
            break;
        case sf::Event::KeyPressed:
            if (ev.key.code == sf::Keyboard::Escape)
                window->close();
            if (ev.key.code == sf::Keyboard::A)
                window->setTitle("Annen");
            break;
        }
    }
}

// FUnctions
void Game::update()
{
    handleEvents();
}

void Game::render()
{
    /*
        renturn void
        
        - clear old frame
        - render objects
        - display frame in window
        render is game objects
    */

    this->window->clear(sf::Color(255, 55, 21,255));
    //Draw Game Objects
    this->window->display();
}

have no idea what I am doing wrong, I looked stackoverflow but i didnt find this error. If someone know how to fix it please help me :).

I didnt tried nothing.Because I didn't understand anything


Solution

  • Field 'window' of 'Game' object is not initialized because the only function called before call 'game.isRunning()' is the empty constructor 'Game::Game()'. So 'window' pointer value is almost random address,that could be outside of virtual memory, and in any case is incorrect. This field should be initialized before call 'game.isRunning()'