SFML community forums
Help => General => Topic started by: Aval on December 27, 2008, 09:34:47 pm
-
I've spent the entire morning trying to get this working, but I still haven't gotten it to stop crashing.
Whenever I close the app, it breaks. In fact, it breaks right after the program hits "return 0;" It gives the following error message in VC++ Express:
First-chance exception at 0x0262cbab in demo.exe: 0xC0000005: Access violation reading location 0xfeeefef6.
Unhandled exception at 0x0262cbab in demo.exe: 0xC0000005: Access violation reading location 0xfeeefef6.
and a runtime error when launched through windows explorer:
Pure virtual function call. :(
In fact, the following code, which simply loads two images and shows them for 5 seconds, crashes at the end.
#define SFML_DYNAMIC
#include <SFML/Graphics.hpp>
int main()
{
sf::Clock timer;
sf::RenderWindow App(sf::VideoMode(200, 200, 32), "Demo App");
App.SetBackgroundColor( sf::Color( 255, 255, 255 ) );
sf::Image Image1, Image2;
if (!Image1.LoadFromFile("pic1.png"))
return 1;
if (!Image2.LoadFromFile("pic2.png"))
return 1;
sf::Sprite Sprite1(Image1);
sf::Sprite Sprite2(Image2);
sf::Sleep( 1.0 );
//Not drawing the sprites fixes the problem, but I don't want a blank game.
while( App.IsOpened() )
{
App.Draw(Sprite1);
App.Draw(Sprite2);
App.Display();
if( timer.GetElapsedTime() > 5.0 )
App.Close();
}
//Crashes right here.
return 0;
}
If I don't call App.Close(), the errors don't happen, but I'm pretty sure that App.Close() does some cleanup work that I shouldn't not do. I must be doing something wrong, in my code. It only happens when I use more than one image and sprite. Help!
-
sf::Window::Close is called in the destructor of sf::Window, so, since you're calling it too, it's trying to delete itself twice.
-
So I don't have to call App.Close() manually?
-
Mmhmm...
-
Wow. I spent a day trying to fix that. I had to remove one line of code to do so. :x
-
I think the pong sample needs to be fixed.
-
I call Close() without problems, if you were not suppose to call it why would it be public? It is used in the examples as well.
-
Perhaps you use different versions of SFML. :?:
-
I just clicked the download now button on the main page, and clicked the first download link for the full sdk on windows. I get the error when using App.Close() using the precompiled binaries, statically linked, or if I compile them myself.
-
Try running an application with the following code and post the results:
int main()
{
delete NULL;
}
-
The code that you gave doesn't compile, as the compiler complains about deleting non-pointers.
but the following code:
int main()
{
int *p;
delete p;
}
gets the same error message that I get.
EDIT:
I looked through the source. The sf::Window class, which sf::RenderWindow inherits from, does call Close() at its destructor. But constructors and destructors aren't inherited, right? Anyways, the destructor for a RenderWindow is this:
RenderWindow::~RenderWindow()
{
// Nothing to do...
}
The Close() is not called when I use RenderWindows like in the example I posted near the top, so it shouldn't be causing the error.
EDIT2:
Man, I feel stupid. Apparently, destructors are called from the base class as well, just after the constructor for the class that is inheriting. So, RenderWindow's destructor runs, and then Window's destructor runs, when the object is destroyed. So Close() is called, after all, in the destruction process.
-
The code you posted earlier works fine for me, but you are using an older version as...
SetBackgroundColor()
...has been replaced by...
Clear()
...if I'm not mistaken.
Have you tried the latest source?
-
Using the newest source doesn't help. I just swapped out the SetBackgRoundColor() at the beginning with a Clear() every frame.
Wizzard is correct that if you call App.Close() manually, it will be called twice: once by you and once by the destructor. Not calling it makes everything better, but I'm not sure why you don't get the error message.
-
Calling Close() multiple times is not a problem, the internal code takes care of that. All the samples and test programs are using this method, and never crash.
-
That explains why when I only have one sprite, there is no error, but it doesn't explain why, when I have more than one, calling Close() causes problems.
-
Are you using the pre-compiled libraries or are you compiling them yourself?
-
Did you test running the same code with static libraries?
-
Hmm. With the dynamic libraries, sometimes I get the error even without an App.Close(). I must be setting something up wrongly. Can anyone send me a Visual C++ Express project? I must have a linker error in there somewhere.
I've tried static and compiling it myself, but I'll try it again.
EDIT:
I've downloaded some SFML projects, and they run fine. So I must be setting something up wrong.
Can someone test this demo on their PC? It's just the source and images in a zip file for a really simple program that crashes on exit on my computer.
http://www.filesavr.com/demo_1
-
I have the same problem, the same exe crash on XP but don“t in Vista...
SFML 1.4 dynamic and precompiled from the official release.
EDIT: I see its in the tasklist.