Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Texture - Unhandled Exception  (Read 4711 times)

0 Members and 1 Guest are viewing this topic.

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
sf::Texture - Unhandled Exception
« 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:
{much better code}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Texture - Unhandled Exception
« Reply #1 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.

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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Texture - Unhandled Exception
« Reply #2 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.
Laurent Gomila - SFML developer

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
sf::Texture - Unhandled Exception
« Reply #3 on: August 23, 2011, 10:28:11 pm »
Thanks guys, food for thought.
{much better code}