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

Author Topic: SFML 1.6 and 2.0 - Unhandled Exception/Access Violation  (Read 4543 times)

0 Members and 1 Guest are viewing this topic.

kurozael

  • Newbie
  • *
  • Posts: 11
    • View Profile
SFML 1.6 and 2.0 - Unhandled Exception/Access Violation
« on: April 12, 2011, 03:55:20 pm »
Okay, let me clear a few things up:

- I've compiled for VS 2010, and am dynamically linking.
- I have all the VS 2010 compiled .dlls in the distro dir.
- I have all the VS 2010 compiled .libs linked to the project.
- I am only linking to debug libs in Debug mode, and same for release.



I have tried with both 1.6 and 2.0. I can get everything to compile and run fine, I have my window, and my 3D OpenGL cubes and spheres spinning about contently.

However:

When I want to have, say, an sf::Image as a member variable of a class (Engine, in the above picture), and then I call something like... say... LoadFromFile on it, I get an unhandled exception/access violation error when I run the executable.





Please help me somebody, I need to get this working ASAP for my university course and I'm tearing my hair out.

It would do the same thing if it were an sf::RenderWindow I was to have as a member variable.

Please don't moan at me for not searching the forum, or Google, because I assure you I've been searching the net for the past 4 hours straight and I have like 28 tabs open because of it. Yet I haven't fixed my problem.

Thank you.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML 1.6 and 2.0 - Unhandled Exception/Access Violation
« Reply #1 on: April 12, 2011, 04:08:05 pm »
Is your Engine instance created before main()?

Can you show the call stack?
Laurent Gomila - SFML developer

kurozael

  • Newbie
  • *
  • Posts: 11
    • View Profile
SFML 1.6 and 2.0 - Unhandled Exception/Access Violation
« Reply #2 on: April 12, 2011, 04:11:31 pm »
Code: [Select]

int main(int argc, char** argv)
{
Engine* engine = Engine::Instance();
engine->Init();

    return EXIT_SUCCESS;
}



Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML 1.6 and 2.0 - Unhandled Exception/Access Violation
« Reply #3 on: April 12, 2011, 04:17:29 pm »
Which revision of SFML 2 are you using?
Laurent Gomila - SFML developer

kurozael

  • Newbie
  • *
  • Posts: 11
    • View Profile
SFML 1.6 and 2.0 - Unhandled Exception/Access Violation
« Reply #4 on: April 12, 2011, 04:27:57 pm »
Quote from: "Laurent"
Which revision of SFML 2 are you using?


LaurentGomila-SFML-df68742 (SFML 2.0 Snapshot)

but to re-iterate I had the same problem with SFML 1.6.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML 1.6 and 2.0 - Unhandled Exception/Access Violation
« Reply #5 on: April 12, 2011, 04:38:02 pm »
Does the following code produce the same crash?
Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
    sf::Image image;
    image.Create(10, 10);

    return 0;
}
Laurent Gomila - SFML developer

kurozael

  • Newbie
  • *
  • Posts: 11
    • View Profile
SFML 1.6 and 2.0 - Unhandled Exception/Access Violation
« Reply #6 on: April 12, 2011, 04:54:38 pm »
Quote from: "Laurent"
Does the following code produce the same crash?
Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
    sf::Image image;
    image.Create(10, 10);

    return 0;
}


Nope, like I said my problem seems to be when sf::Image is a class member variable.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML 1.6 and 2.0 - Unhandled Exception/Access Violation
« Reply #7 on: April 12, 2011, 05:02:09 pm »
Technically this makes no difference.

How is Engine::Instance() implemented?
Laurent Gomila - SFML developer

kurozael

  • Newbie
  • *
  • Posts: 11
    • View Profile
SFML 1.6 and 2.0 - Unhandled Exception/Access Violation
« Reply #8 on: April 12, 2011, 05:08:06 pm »
Code: [Select]

class Test
{
public:
Test() {};
~Test() {};
void Blah();
sf::RenderWindow m_window;
};

void Test::Blah()
{
m_window.Create(
sf::VideoMode(800, 600, 32), "Game",
sf::Style::Titlebar | sf::Style::Close
);
}

int main()
{
Test lol;

lol.Blah();

while ( lol.m_window.IsOpened() )
{
sf::Event windowEvent;

while ( lol.m_window.PollEvent(windowEvent) )
{
if (windowEvent.Type == sf::Event::Closed)
lol.m_window.Close();
}

lol.m_window.Clear();
lol.m_window.Display();
}

return 0;
}


Works fine... so could it be that storing sf:: objects such as RenderWindow or Image within a class that is stored in the "magic static place" e.g: my singleton Engine class causes the crash/memory violation error?

If so, can this be fixed?

kurozael

  • Newbie
  • *
  • Posts: 11
    • View Profile
SFML 1.6 and 2.0 - Unhandled Exception/Access Violation
« Reply #9 on: April 12, 2011, 05:10:15 pm »
Okay yeah the "magic static place" is causing the crash, any ideas?

Code: [Select]

#include <SFML/Graphics.hpp>

class Test
{
public:
static Test* Instance();
Test() {};
~Test() {};
void Blah();
sf::RenderWindow m_window;
};

Test* Test::Instance()
{
static Test* test;
return test;
}

void Test::Blah()
{
m_window.Create(
sf::VideoMode(800, 600, 32), "Game",
sf::Style::Titlebar | sf::Style::Close
);
}

int main()
{
Test* lol = Test::Instance();

while ( lol->m_window.IsOpened() )
{
sf::Event windowEvent;

while ( lol->m_window.PollEvent(windowEvent) )
{
if (windowEvent.Type == sf::Event::Closed)
lol->m_window.Close();
}

lol->m_window.Clear();
lol->m_window.Display();
}

return 0;
}

kurozael

  • Newbie
  • *
  • Posts: 11
    • View Profile
SFML 1.6 and 2.0 - Unhandled Exception/Access Violation
« Reply #10 on: April 12, 2011, 05:40:21 pm »
Fixed by this:

Code: [Select]

static T* m_pInstance =  0;
if (!m_pInstance)
m_pInstance = new T;

return m_pInstance;


(Creating it on the heap).

Thanks for your help!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
SFML 1.6 and 2.0 - Unhandled Exception/Access Violation
« Reply #11 on: April 12, 2011, 05:42:08 pm »
The pointer you declare inside Instance() remains uninitialized. Dereferencing it evokes undefined behavior.

Just use an object instead of a pointer.
Code: [Select]
static X& Instance()
{
    static X obj;
    return obj;
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: