SFML community forums

Help => General => Topic started by: ansh93 on November 02, 2012, 09:06:52 pm

Title: Unhandled exception on window creation
Post by: ansh93 on November 02, 2012, 09:06:52 pm
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include <SFML\Window.hpp>
#include <iostream>

using namespace std;

int main()
{
        int x=0,y=0;

        sf::VideoMode myMode(640,480,32);
        sf::RenderWindow myWindow(myMode,"board test");

        sf::Texture myTexture;
        if(!myTexture.loadFromFile("C:\\Users\\Voldy\\Documents\\Blue Tile.png"))
        {
                cout << " Could not load image" << endl ;
                return 0;
        }
        sf::Sprite mySprite(myTexture);
        sf::Event event;

        while (myWindow.isOpen())
        {
                while (myWindow.pollEvent(event))
                {
                        if(event.type == sf::Event::Closed || event.key.code == sf::Keyboard::Escape)
                        {
                                myWindow.close();
                        }
                        sf::IntRect myRect(x,y,32,32);
                        mySprite.setTextureRect(myRect);
                        myWindow.clear();
                        myWindow.draw(mySprite);
                        myWindow.display();
                }
        }


}

This is the code i am trying to run everything works fine
but when I try to run it i get an error saying "Unhandled exception at 0x75a6f7cc in board render tes.exe: 0xC0000005: Access violation reading location 0x65742064.".
previously i was able to run the projects of sfml
no such error occurred.
now also i use the old projects and just write the code in them and works perfectly fine but if i make any new project i get this error.
please help me to solve this error
Title: Re: Unhandled exception on window creation
Post by: FRex on November 02, 2012, 09:17:21 pm
event.key.code == sf::Keyboard::Escape
Try removing that first. Did you step through code with debugger to see where exactly it crashes?
Title: Re: Unhandled exception on window creation
Post by: ansh93 on November 03, 2012, 07:29:31 am
yah i already saw the debugger
it crashes at the line for window creation
Title: Re: Unhandled exception on window creation
Post by: FRex on November 03, 2012, 12:53:52 pm
Are you sure you're not using .dlls and .libs compiled with other version of your compiler?
Title: Re: Unhandled exception on window creation
Post by: cooldog99 on November 03, 2012, 01:00:06 pm
This is the code i am trying to run everything works fine
but when I try to run it i get an error saying "Unhandled exception at 0x75a6f7cc in board render tes.exe: 0xC0000005: Access violation reading location 0x65742064.".
previously i was able to run the projects of sfml
no such error occurred.
now also i use the old projects and just write the code in them and works perfectly fine but if i make any new project i get this error.
please help me to solve this error


As far as creating at creation of window, i'm not 100% sure.
but there is ONE problem with your code.

You have these lines:

            sf::IntRect myRect(x,y,32,32);
            mySprite.setTextureRect(myRect);
            myWindow.clear();
            myWindow.draw(mySprite);
            myWindow.display();
 

inside the second while loop, when it should be under :)

so it'll be this:
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include <SFML\Window.hpp>
#include <iostream>

using namespace std;

int main()
{
        int x=0,y=0;

        sf::VideoMode myMode(640,480,32);
        sf::RenderWindow myWindow(myMode,"board test");

        sf::Texture myTexture;
        if(!myTexture.loadFromFile("C:\\Users\\Voldy\\Documents\\Blue Tile.png"))
        {
                cout << " Could not load image" << endl ;
                return 0;
        }
        sf::Sprite mySprite(myTexture);
        sf::Event event;

        while (myWindow.isOpen())
        {
                while (myWindow.pollEvent(event))
                {
                        if(event.type == sf::Event::Closed || event.key.code == sf::Keyboard::Escape)
                        {
                                myWindow.close();
                        }

                }
                        sf::IntRect myRect(x,y,32,32);
                        mySprite.setTextureRect(myRect);
                        myWindow.clear();
                        myWindow.draw(mySprite);
                        myWindow.display();
        }


}


As FRex said, make sure your linker and includes are correct.

Maybe rebuild SFML lib, and remove all old SFML (if any)