Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Error is driving me nuts

Started by Camilos Nov 1, 2014 at 12:51 PM 38 replies 14.4k views
Original Post
Camilos
Camilos

ok, i've been trying to create a pong game but i keep having this error and i don't know how to fix it.

Here is the code


#include "Game.h"
#include "Basic.h"


Game::GameState Game::gameState = Uninitialized;
sf::RenderWindow Game::MainWindow;
void Game::Start(){
	if(gameState != Uninitialized){
		return;}
	else {  
		MainWindow.create(sf::VideoMode(1024,768,32),"Pong");
		gameState = Game::Playing;	
	};
	
	while(!Exit()){
		GameLoop();
	}
	MainWindow.close();

};
bool Game::Exit(){
	if(gameState = Exiting){
		return true;}
	else{return false;};
};

void Game::GameLoop(){
	sf::Event Event;
	if(gameState = Playing){
		while(MainWindow.pollEvent(Event)){
			MainWindow.clear(sf::Color::Black);
			MainWindow.display();

			if((Event.type == sf::Event::Closed)||(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))){
				gameState = Game::Exiting;
			};

		};}



};

and the game.h file


#include "Basic.h"

class Game{
public: 
	 static void Start();
	 static bool Exit();
	 static void GameLoop();
	 enum GameState{Uninitialized, ShowingSplash, Paused, 
          ShowingMenu, Playing, Exiting };

	 static GameState gameState;
	 static sf::RenderWindow MainWindow;



};

I keep having the error '"Unhandled exception at 0x77cad062 in Pong.exe: 0xC0000005: Access violation writing location 0x00000004". while running it, can somebody please help me

rip-off
rip-off

Oh noes, my code is teh crash!

I would note that in a few places you're using assignment rather than an equality check:


if (a = b) {
   // ....
}

Code that looks like the above sets a to b's value, then tests whether a's value is false.

So this code:


if (gameState = Exiting) {
    return true;
}

gameState will be set to "Exiting". Exiting is non-zero so the return statement would then be executed.

yckx
yckx
Have you tried a debug build? Debugging should give you a more detailed error message.

Are you sure the error occurs in this file? You're missing a main() so I'm guessing you have more code.

Also, your if conditions in Game::Exit() and Game::GameLoop() don't do what you think they do. You're setting game State instead of testing it.
Camilos
Camilos

could the problem come from the basic.h :


#include <SFML\Graphics.hpp>
#include <SFML\Audio.hpp>
#include <SFML\Window.hpp>
#include <SFML\System.hpp>

#include <iostream>
#include <ctime>
#include <map>
#include <cmath>
#include <cstdlib>
#include <cassert>
rip-off
rip-off

Have you tried running the code in a debugger yet?

Buckeye
Buckeye

1. Read through the information in the link rip-off provided.

2. Determine which line of code causes the error, or, at a minimum, in which group of 3 or 4 lines of code the error occurs.

In general, when you encounter an error, go through your code once or twice looking for obvious typos or logic errors. If that doesn't resolve the problem, debug your code by setting breakpoints, examining values, determining which routines work correctly, etc.

EDIT: As mentioned several times above, running your code in a good debugging IDE will likely result in the debugger indicating which line the error occurs.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.
Camilos
Camilos

can you give me a link to a good debugger?

Eck
Eck

Many IDE's have one built in. If you're using Visual Studio, you already have one. Put your cursor at line 6 and hit F9 to set a break point. Then run your code and it should show you line by line what is happening. Hit F10 to execute that line and go to the next line or F11 to call into that line and go deeper into the call stack (like into a function). Use the Locals or Watch Window to inspect variables.

If you aren't using Visual Studio, I recommend Googling MyDevelopmentTool Debugger and figure out how to do it there.

Usually an error like that means you're assigning something to a null pointer and the debugger will show you exactly where the problem is.

- Eck

Camilos
Camilos

found the problem:


Game::GameState Game::gameState = Uninitialized;
sf::RenderWindow Game::MainWindow;

i'm currently looking into it

Camilos
Camilos

screw it, going to try a different guide

rip-off
rip-off

Ok, just remember you're going to run into this kind of problem eventually, and you'll need to learn how to fix it. It is an important part of learning C++.

Can you describe what you mean by "found the problem"? Do you have a stack trace of the crash?

I was able to get the guide you posted to work on Linux, but it is a little different from the code you posted (i.e. there is no "Basic.h"). Can you get the exact code in the guide to compile and run successfully?

Camilos
Camilos

I think that there is a problem with

Game::GameState Game::gameState = Uninitialized;

because that's where it stops when the error appears but i don't know why, if somebody can help me, please do because I can't get it out of my head

yckx
yckx
Can you post what information the debugger gives you when it stops there? It should give you a stack trace and a detailed message of why it stopped there.
Camilos
Camilos

I don't know if this helps but when started pong again this appeared:

Visual C++ found a suitable location to store its browsing database and IntelliSense files for the solution "C:\Users\Ruben\documents\visual studio 2010\Projects\Pong\Pong.sln."
Visual C++ examined the folder "C:\Users\Ruben\documents\visual studio 2010\Projects\Pong." This folder is not suitable because of the following:
The browsing database in this directory is open by another instance of Visual Studio and cannot be reopened. User Ruben on RUBEN-HP has it open.
Because a 'Fallback Location' was not specified in the C++ Advanced Options, Visual C++ is attempting to use your temporary directory.
Visual C++ examined the folder "C:\Users\Ruben\AppData\Local\Temp\VC++\pong-7e910abd." This folder is suitable because of the following:
The directory is on a local drive.
The 'Fallback Location' is configurable under C++ Advanced Options.
Press OK to use this location.
Press Cancel to disable C++ browsing information and IntelliSense for this session.
Camilos
Camilos

currently trying something but don't know if it will work so if you know something, please tell me

Camilos
Camilos

I know that there is something wrong with Game::GameState Game::gameState = Uninitialized; because that's when the error appears and i get this in the output:

First-chance exception at 0x77cad062 in pong.exe: 0xC0000005: Access violation writing location 0x00000004.
Unhandled exception at 0x77cad062 in pong.exe: 0xC0000005: Access violation writing location 0x00000004.
anybody know's how to fix this?
PS: maybe somebody can run the code and see the problem themselve
frob
frob

I know that there is something wrong with Game::GameState Game::gameState = Uninitialized; because that's when the error appears and i get this in the output:
First-chance exception at 0x77cad062 in pong.exe: 0xC0000005: Access violation writing location 0x00000004.
Unhandled exception at 0x77cad062 in pong.exe: 0xC0000005: Access violation writing location 0x00000004.

That specific error is that you are following a null pointer, and writing data to it.

On modern operating systems the range from 0x00000000 up through a fairly high number are all reserved. NULL on these operating systems is also memory address zero, so you doing a little math, since many variables are at a 4-byte step, you are almost certainly setting the second variable of a structure or class where the pointer is null. For example, the line might be myPtr->foo = value; where myPtr is 0 instead of a valid pointer.

Your debugger should tell you the line of code that corresponds to the code address 0x77cad062. If your debugger doesn't tell you for some reason, those useful little debugging files the compiler generates, such as map files, can tell you as well.

Camilos
Camilos

I kind of guessed this was the case, but i don't understand why it is a null pointer or how to fix it.

rip-off
rip-off

I also don't understand how the line of code being referenced is the root cause of that error.

Are you sure you're looking at the correct file? Sometimes it is possible to end up with multiple copies of a file, one of which you're editing and another which ends up being compiled. Are you sure the executable you're running is up to date? Some IDEs make it easy to run the old executable when building a new one fails.

Have you tried a simpler program, such as placing all the code in a single file, something like:


#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
 
#include <iostream>
#include <ctime>
#include <map>
#include <cmath>
#include <cstdlib>
#include <cassert>
 
enum GameState {
    Playing,
    Exiting
};
 
int main() {
    sf::RenderWindow MainWindow;
    MainWindow.create(sf::VideoMode(1024, 768, 32), "Pong");
    GameState gameState = Playing;
    while (gameState != Exiting) {
        sf::Event Event;
        while (MainWindow.pollEvent(Event)) {
            if ( (Event.type == sf::Event::Closed) || (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) ) {
                gameState = Exiting;
            }
        }
        MainWindow.clear(sf::Color::Black);
        MainWindow.display();
    }
}
 

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.