SFML community forums
General => General discussions => Topic started by: kurozael 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.
(http://i.imgur.com/ycYDY.png)
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.
(http://i.imgur.com/4fyxz.png)
(http://i.imgur.com/NcojR.png)
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.
-
Is your Engine instance created before main()?
Can you show the call stack?
-
int main(int argc, char** argv)
{
Engine* engine = Engine::Instance();
engine->Init();
return EXIT_SUCCESS;
}
(http://i.imgur.com/jDcoW.png)
-
Which revision of SFML 2 are you using?
-
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.
-
Does the following code produce the same crash?
#include <SFML/Graphics.hpp>
int main()
{
sf::Image image;
image.Create(10, 10);
return 0;
}
-
Does the following code produce the same crash?
#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.
-
Technically this makes no difference.
How is Engine::Instance() implemented?
-
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?
-
Okay yeah the "magic static place" is causing the crash, any ideas?
#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;
}
-
Fixed by this:
static T* m_pInstance = 0;
if (!m_pInstance)
m_pInstance = new T;
return m_pInstance;
(Creating it on the heap).
Thanks for your help!
-
The pointer you declare inside Instance() remains uninitialized. Dereferencing it evokes undefined behavior.
Just use an object instead of a pointer.
static X& Instance()
{
static X obj;
return obj;
}