1
General discussions / Memory Access violation after program closes
« on: July 17, 2008, 10:08:18 pm »Quote from: "dabo"
I'm using the static-libraries without any problems. My class is a singleton as well.
Yes, but is your singleton like mine listed above? Or are you returning a pointer. ie.
Code: [Select]
static ImageManager* GetInstance()
{
if(instance == 0) { instance = new instance(); }
return instance;
}
This version did not produce the access violation while using the static libs. But I didn't want to use this version because there was a danger of memory leak. I tried wrapping the pointer with a Boost::shared_ptr but that produced the same access violation at the end of the program.
Back to the original version in my post (using static libs).
When I was trying to track down the problem. I put a cout message and _getche() in the ImageManager's destructor like this:
Code: [Select]
~ImageManager() { std::cout << "IM Destructor..." << std::endl; _getche(); }
After the call to App.Close(), the window would close, leaving me with just the console. The console showing my message "IM Dstructor..." and the prompt waiting for me to hit any key.
Once I hit any key, the access violation occurred.
My main.cpp is typical: Psuedocode
Code: [Select]
int main() do
initialize App
sf::Sprite mySprite(ImageLoader::GetInstance().getImage(filename)));
while(App.Open()) do // Game Loop
while(Events in Queue) do
if(Event == Event.Close)
App.Close;
break;
end while
//my render stuff
end while
return EXIT_SUCCESS
//end main
Here is what I believe is going on:
1) App.Close() is invoked, Performs it's window clean up stuff
2) Game loop is exited
3) The Sprites in main.cpp destructors are called
4) The static ImageManager destructor is called
5) Access Violation Occurs
Steps 3 and 4 might be reversed.
When step 3 takes place the sf::Image's underlying texture, pixels, or videoresource is released. Then in step 4 the application/system tries to release those same resources again.
This should not occur because the owner of the sf::Image is the std::map inside the static ImageManger. The Sprites are only holding references to those Images.
Laurent, I hope this is info is helpful.
And thanks to you and to all who develop SFML - very cool Library!!!
-Shilo