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

Author Topic: Access Violation at Program Close  (Read 9882 times)

0 Members and 1 Guest are viewing this topic.

Aval

  • Newbie
  • *
  • Posts: 34
    • View Profile
    • Email
Access Violation at Program Close
« 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.

 
Code: [Select]

#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!

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Access Violation at Program Close
« Reply #1 on: December 27, 2008, 11:51:17 pm »
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.

Aval

  • Newbie
  • *
  • Posts: 34
    • View Profile
    • Email
Access Violation at Program Close
« Reply #2 on: December 27, 2008, 11:55:30 pm »
So I don't have to call App.Close() manually?

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Access Violation at Program Close
« Reply #3 on: December 27, 2008, 11:57:45 pm »
Mmhmm...

Aval

  • Newbie
  • *
  • Posts: 34
    • View Profile
    • Email
Access Violation at Program Close
« Reply #4 on: December 28, 2008, 12:15:47 am »
Wow. I spent a day trying to fix that. I had to remove one line of code to do so.  :x

Aval

  • Newbie
  • *
  • Posts: 34
    • View Profile
    • Email
Access Violation at Program Close
« Reply #5 on: December 28, 2008, 12:17:52 am »
I think the pong sample needs to be fixed.

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
Access Violation at Program Close
« Reply #6 on: December 28, 2008, 12:22:23 pm »
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.

irri

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • http://www.irri.se
Access Violation at Program Close
« Reply #7 on: December 28, 2008, 01:24:49 pm »
Perhaps you use different versions of SFML. :?:
2D RPG Game (School project): http://PA.irri.se/

Aval

  • Newbie
  • *
  • Posts: 34
    • View Profile
    • Email
Access Violation at Program Close
« Reply #8 on: December 28, 2008, 03:18:01 pm »
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.

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Access Violation at Program Close
« Reply #9 on: December 29, 2008, 12:15:20 am »
Try running an application with the following code and post the results:
Code: [Select]
int main()
{
    delete NULL;
}

Aval

  • Newbie
  • *
  • Posts: 34
    • View Profile
    • Email
Access Violation at Program Close
« Reply #10 on: December 29, 2008, 01:13:47 am »
The code that you gave doesn't compile, as the compiler complains about deleting non-pointers.

but the following code:

Code: [Select]

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:

 
Code: [Select]

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.

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
Access Violation at Program Close
« Reply #11 on: December 29, 2008, 03:06:56 am »
The code you posted earlier works fine for me, but you are using an older version as...

Code: [Select]
SetBackgroundColor()

...has been replaced by...

Code: [Select]
Clear()

...if I'm not mistaken.

Have you tried the latest source?

Aval

  • Newbie
  • *
  • Posts: 34
    • View Profile
    • Email
Access Violation at Program Close
« Reply #12 on: December 29, 2008, 05:06:26 pm »
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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Access Violation at Program Close
« Reply #13 on: December 29, 2008, 05:34:34 pm »
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.
Laurent Gomila - SFML developer

Aval

  • Newbie
  • *
  • Posts: 34
    • View Profile
    • Email
Access Violation at Program Close
« Reply #14 on: December 30, 2008, 01:35:37 am »
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.

 

anything