SFML Crash Confusion

I've recently started learning SFML. Unfortunately, whenever my "game" ends, the program crashes. I've linked this to my Game deconstructor (and discovered that in general, whenever a Food object, dynamically allocated or not, is deleted, the program crashes). The game will hopefully evolve into, at least an attempt at Snake. So if you understand why it's crashing, well I would greatly appreciate some advice, thanks!

main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <cstdlib>
#include "Game.h"

int main(int argc, char *argv[])
{
	Game game;
	while (game.Continue()) {
		game.Update();
	}
	return EXIT_SUCCESS;
}

Game.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once

#include "Food.h"
#include <SFML/System.hpp>

class Game {		
public:
	Game(void);
	~Game(void);

	bool Continue(void) const { return App.IsOpened(); }
	void HandleEvents(void);
	void Update(void);
private:
	Food *food;
	sf::RenderWindow App;
};

extern const int ScreenWidth;
extern const int ScreenHeight;

Game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "Game.h"

// screen dimensions... duh
const int ScreenWidth = 300;
const int ScreenHeight = 300;

// constructor
Game::Game(void)
	: App(sf::VideoMode(ScreenWidth, ScreenHeight, 32), std::string(), sf::Style::Close)
{ 
	food = new Food(sf::Randomizer::Random(0, ScreenWidth), 
					sf::Randomizer::Random(0, ScreenHeight));
	App.SetFramerateLimit(60);		// easy on the FPS l33tz0r
}


Game::~Game(void)
{
	// free up memory
	delete food;
	food = NULL; 
}

void Game::HandleEvents(void)
{
	// handle basic events (i.e. keypresses)
	sf::Event Event;
	while (App.GetEvent(Event)) {
		// if the game is closed
		if (Event.Type == sf::Event::Closed)
			App.Close();
	}
}

void Game::Update(void)
{
	HandleEvents();
	App.Clear();
	food->Render(App);
	App.Display();
}

Food.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#pragma once

#include <SFML/Graphics.hpp>

class Food {
public:
	Food(float, float);
	~Food()
	{  }

	void Render(sf::RenderWindow &App) { App.Draw(shape); }
private:
	static const int width = 5;
	static const int height = 5;
	sf::Shape shape;
};

1
2
3
4
5
6
#include "Food.h"

// constructor
Food::Food(float x, float y)
	: shape(sf::Shape::Rectangle(x, y, x+width, y+height, sf::Color(255, 0, 0)))
{ }


Sorry to plop all this code here, but I have no idea what the source of the problem is, so I don't know what specific parts to highlight :/

Thank you!
Last edited on
Bump, someone please help!
whenever a Food object, dynamically allocated or not, is deleted, the program crashes
Only delete what you new

In this case you better use an object instead of a pointer.

I could not reproduce your crash.
Only delete what you new

Look at my code... that's exactly what I was doing. I guess I'll try it again not dynamically allocating it, and I'll see if it works better this time :/

EDIT: I forgot to mention that it's the console that crashes after the window closes.

EDIT (again): Wow... I just fixed it by putting an exit(0); in... problem resolved :)
Last edited on
Probably not. You may have incurred in undefined behaviour.
¿Is that all your code?
Probably not.

That fixed the problem, so I'm good now. And yup that's the whole thing.
If I had to guess:

1
2
3
4
5
6
7
void Game::Update(void)
{
	HandleEvents();
	App.Clear();
	food->Render(App);
	App.Display();
}


App can be closed in HandleEvents(), but you continue to use it.



Topic archived. No new replies allowed.