SFML community forums

Help => General => Topic started by: leopoldo100 on February 17, 2021, 01:42:55 am

Title: Access violation error? (sfml-window-d-2.dll)
Post by: leopoldo100 on February 17, 2021, 01:42:55 am
I am a beginner in C++ and trying to learn how to make some simple 2D games. I've already linked all the files and tested it beforehand, to which it ran successfully.

After writing this code, when trying to run it, I keep getting the following error:
Exception thrown at 0x6214836A (sfml-window-d-2.dll) in finalTest.exe: 0xC0000005: Access violation reading location

Here is my main.cpp: (please ignore the comments, I'm learning so they help me see what I'm doing)
Code: [Select]
#include <iostream>
#include "Game.h"

using namespace std;
int main() {
//Init Game Engine
Game game;
//Game loop
while (game.running())
{

//Update
game.update();

//Render
game.render();
}

//End of application
return 0;
}

Here is my Game.h:
Code: [Select]
#pragma once

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

/*
Class that acts as the game engine.
Wrapper class.
*/

class Game
{
public:
//Constructors / Destructors
Game();
virtual ~Game();

//Accessors
const bool running() const;

//Functions
void pollEvents();
void update();
void render();



private:
//Variables
//Window
sf::RenderWindow* window;
sf::VideoMode videoMode;
sf::Event ev;

//Private functions
void initVariables();
void initWindow();
};

Here is my game.cpp:
Code: [Select]
#include "Game.h"

//Constructors / Destructors
Game::Game()
{
this->initVariables();
this->initWindow();
}

Game::~Game()
{
delete this->window;
}

const bool Game::running() const
{
return this->window->isOpen(); [this is where I'm getting the error]
}

//Functions
void Game::pollEvents()
{
//Event polling
while (this->window->pollEvent(this->ev))
{
switch (this->ev.type)
{
case sf::Event::Closed:
this->window->close();
break;
case sf::Event::KeyPressed:
if (this->ev.key.code == sf::Keyboard::Escape)
this->window->close();
break;
}
}
}

void Game::update()
{
this->pollEvents();
}

void Game::render()
{
/*
@return void

- clear old frame
- render objects
- display frame in window

Renders the game objects.
*/
this->window->clear(sf::Color(255, 0, 0, 255));

//Draw game objects

this->window->display();
}

//Private functions
void Game::initVariables()
{
this->window == nullptr;
}

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

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

Can someone please tell me what I did wrong?
Title: Re: Access violation error? (sfml-window-d-2.dll)
Post by: Kvaz1r on February 17, 2021, 11:34:59 am
Hello == is comparison not assign. Change
this->window == new sf::RenderWindow(this->videoMode, "Game 1", sf::Style::Titlebar | sf::Style::Close);
to
this->window = new sf::RenderWindow(this->videoMode, "Game 1", sf::Style::Titlebar | sf::Style::Close);
Title: Re: Access violation error? (sfml-window-d-2.dll)
Post by: leopoldo100 on February 18, 2021, 12:52:45 am
Hello == is comparison not assign. Change
this->window == new sf::RenderWindow(this->videoMode, "Game 1", sf::Style::Titlebar | sf::Style::Close);
to
this->window = new sf::RenderWindow(this->videoMode, "Game 1", sf::Style::Titlebar | sf::Style::Close);
Thank you so much! This fixed it.