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

Author Topic: Destruction of the OpenGL context  (Read 4068 times)

0 Members and 1 Guest are viewing this topic.

lolz123

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
Destruction of the OpenGL context
« on: January 21, 2012, 04:22:08 am »
Hello,

I have been plagued for a while by some destruction order issues.
When the program closes, it gives me a memory access violation and points me to line 110 in WglContex.cpp. I assume that the OpenGL context is destroying before sf::Texture and sf::RenderTextures are destroyed, but they are still making calls to the context to delete the textures.  The only solution I have had so far is allocating textures on the heap and explicitly deleting them. Is there a way around this?
Have you heard about the new Cray super computer?  It’s so fast, it executes an infinite loop in 6 seconds.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Destruction of the OpenGL context
« Reply #1 on: January 21, 2012, 09:32:01 am »
Quote
I assume that the OpenGL context is destroying before sf::Texture and sf::RenderTextures are destroyed

Nop, a context will still exist until all OpenGL resources are destroyed.

Can you please post more details about your error?
Laurent Gomila - SFML developer

lolz123

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
Destruction of the OpenGL context
« Reply #2 on: January 21, 2012, 03:37:58 pm »
Unfortunately, I do not know how to reproduce this error with minimal code, since if it is indeed the destruction order, it will occur randomly. Here is my call stack when the application closes, maybe it helps:

Code: [Select]
> sfml-window-d-2.dll!sf::priv::WglContext::~WglContext()  Line 110 + 0xf bytes C++
  sfml-window-d-2.dll!sf::priv::WglContext::`scalar deleting destructor'()  + 0x16 bytes C++
  sfml-window-d-2.dll!sf::priv::GlContext::GlobalCleanup()  Line 128 + 0x29 bytes C++
  sfml-window-d-2.dll!sf::GlResource::~GlResource()  Line 79 C++
  sfml-graphics-d-2.dll!sf::Texture::~Texture()  Line 104 + 0xf bytes C++
  sfml-graphics-d-2.dll!sf::RenderTexture::~RenderTexture()  Line 48 + 0x12 bytes C++
  GF_Multiplayer_Client.exe!LightSystem::~LightSystem()  Line 96 + 0x91 bytes C++
  GF_Multiplayer_Client.exe!`dynamic atexit destructor for 'lSystem''()  + 0x28 bytes C++
  msvcr100d.dll!doexit(int code=0, int quick=0, int retcaller=0)  Line 567 C
  msvcr100d.dll!exit(int code=0)  Line 393 + 0xd bytes C
  GF_Multiplayer_Client.exe!__tmainCRTStartup()  Line 568 C
  GF_Multiplayer_Client.exe!mainCRTStartup()  Line 371 C


I tried various versions of SFML 2, but I get this error with all of them.
Have you heard about the new Cray super computer?  It’s so fast, it executes an infinite loop in 6 seconds.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Destruction of the OpenGL context
« Reply #3 on: January 21, 2012, 03:49:17 pm »
Are you calling exit(0) yourself?
Laurent Gomila - SFML developer

lolz123

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
Destruction of the OpenGL context
« Reply #4 on: January 21, 2012, 03:53:42 pm »
No
Have you heard about the new Cray super computer?  It’s so fast, it executes an infinite loop in 6 seconds.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Destruction of the OpenGL context
« Reply #5 on: January 21, 2012, 05:27:33 pm »
Is the LightSystem instance a global?
Laurent Gomila - SFML developer

lolz123

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
Destruction of the OpenGL context
« Reply #6 on: January 21, 2012, 05:28:45 pm »
Yes, it is a singleton class.
Have you heard about the new Cray super computer?  It’s so fast, it executes an infinite loop in 6 seconds.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Destruction of the OpenGL context
« Reply #7 on: January 21, 2012, 05:29:37 pm »
Don't do this, it produces undefined behaviour since SFML has globals too.
Laurent Gomila - SFML developer

lolz123

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
Destruction of the OpenGL context
« Reply #8 on: January 21, 2012, 05:45:48 pm »
Is there no way around this? Can't the texture destructor just check if the context still exists and not delete the texture manually if there is none? As far as I know, OpenGL deletes textures by itself when the associated context is destroyed, but I may be wrong.
Have you heard about the new Cray super computer?  It’s so fast, it executes an infinite loop in 6 seconds.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Destruction of the OpenGL context
« Reply #9 on: January 21, 2012, 06:11:29 pm »
It's not that simple. What makes it impossible to handle correctly is that SFML globals may be destroyed before yours. And it's impossible to check whether a global has been destroyed or not.

It's generally not a good idea to have global objects that do something with resources in their destructor, so you'd better try to find a better design.
Laurent Gomila - SFML developer

lolz123

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
Destruction of the OpenGL context
« Reply #10 on: January 21, 2012, 06:27:45 pm »
Alright, thanks.
Have you heard about the new Cray super computer?  It’s so fast, it executes an infinite loop in 6 seconds.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Destruction of the OpenGL context
« Reply #11 on: January 21, 2012, 06:29:54 pm »
Quote from: "lolz123"
Is there no way around this?
Avoid global variables. Singletons are often only used because of lazyness and the misbelief that otherwise, parameters would have to be passed all the time. In a good design, there are very few things that really need to be global.

See also this post.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: