SFML community forums

Help => Graphics => Topic started by: Jove on August 22, 2011, 09:28:37 pm

Title: sf::Texture - Unhandled Exception
Post by: Jove on August 22, 2011, 09:28:37 pm
I've just changed to SFML2 from 1.6. Previously in my code I declared all of my sf::Image sprite images outside main() so I don't have to keep passing around loads of refs.

Doing the same with sf::Texture, like this:

Code: [Select]
sf::Texture texture;

int main()
 {
     // Create the main window
     sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

     // Load a sprite to display
// sf::Texture texture;

     if (!texture.LoadFromFile("ball.png"))
         return EXIT_FAILURE;

sf::Sprite sprite(texture);


causes the first line above to throw "Unhandled exception at 0x775f22c2 in Window Events.exe: 0xC0000005: Access violation writing location 0x00000004." at :

Code: [Select]
void MutexImpl::Lock()
{
    EnterCriticalSection(&myMutex);
}



Unusual? Or, just me being a lousy programmer?  :oops:
Title: Re: sf::Texture - Unhandled Exception
Post by: Nexus on August 22, 2011, 09:59:13 pm
Quote from: "Jove"
Previously in my code I declared all of my sf::Image sprite images outside main() so I don't have to keep passing around loads of refs.
You shouldn't do that, even if it appears to be convenient in the first place. See also this post (http://www.sfml-dev.org/forum/viewtopic.php?p=34227#34227).

If you avoid global variables, you don't have to pass around references all the time. If you keep dependencies between classes small, then fewer object propagations are necessary. You can also group multiple arguments together (e.g. in a container) and store them as a member as soon as you initialize the object.

I'm not sure about your problem, though. Have you recompiled and linked SFML correctly? It might be that the global texture gets in conflict with other global data provided by SFML, namely the OpenGL context. At least, the error message looks like a dependent object hasn't been initialized correctly yet.
Title: sf::Texture - Unhandled Exception
Post by: Laurent on August 22, 2011, 10:01:59 pm
The reason of the crash is that SFML also uses global objects, and in this case some of them are constructed after yours.
Title: sf::Texture - Unhandled Exception
Post by: Jove on August 23, 2011, 10:28:11 pm
Thanks guys, food for thought.