   glDeleteTextures and the OpenGL context
  Hi,When the OpenGL context is destroyed, does it also delete all textures?Normally I think that to avoid memory leaks, for every glGenTextures, there should be also a glDeleteTextures to free the textures again.But sometimes the OpenGL context is destroyed (for example when exiting the program).Before the GL context is destroyed, do you need to call glDeleteTextures for all textures, or does the destruction of the context already free everything automatically?
  Do you need to? No. I would, however, strongly advice you write your program to clean up after itself.If you wrap resources correctly, you may not have to do anything in order to clean up after yourself, similar to how smart pointers clean up after themselves when they go out of scope. In that sense, cleaning up when the program exits is no different than cleaning up old resources when loading new ones when you load a new level. You would not think about leaving those resources floating around until the user exits the program, would you?   ;Quote:Original post by Brother BobDo you need to? No. I would, however, strongly advice you write your program to clean up after itself.If you wrap resources correctly, you may not have to do anything in order to clean up after yourself, similar to how smart pointers clean up after themselves when they go out of scope. In that sense, cleaning up when the program exits is no different than cleaning up old resources when loading new ones when you load a new level. You would not think about leaving those resources floating around until the user exits the program, would you?I don't have an Amiga myself, but people using AmigaOS4 are trying to compile my program. They discovered the following: on Windows and Linux, when you call glDeleteTextures after the GL context was destroyed, there is no problem. On AmigaOS4 however, it crashes if you call glDeleteTextures after the GL context was destroyed.So basically, I'm in a very messy situation: behaviour of SDL and OpenGL that is different on 3 different operating systems.This problem is combined with the fact that I can't correctly know for sure whether or not the GL context was deleted and recreated after doing SDL_SetVideoMode (see my other thread here).Basically, calling or not calling glDeleteTextures can result in white textures, a crash, or a memory leak depending on the combination of version of SDL and OS.I also found at OpenGL.org itself, that a ctor in a class that does gentexture and a dtor that does deletetextures is NOT a good idea. http://www.opengl.org/wiki/Common_MistakesSo in fact I'm trying to have a system that:-can create opengl textures-deletes opengl textures if needed to avoid memory leaks-does NOT crash when deleting textures-supports resizing of the window-depending on whether or not SDL_SetVideoMode destroyed the gl context, recreates the textures (and either has or has not to glDelete the ones from the previous context???)-works on linux, windows, mac, and amigaos4   ;  I had the same trouble and found that it doesn't matter if you call glDeleteTextures or not before you delete the context. What does matter is calling it AFTER the context has been deleted, more specifically with a new context, as the textures don't exist any more.If you just assume that the context is destroyed and recreated, you should be fine. (Delete textures / buffers before calling SDL_SetVideoMode OR just make sure glDeleteTextures isn't called afterwards, then recreate them all).Possibly just put a reload member function in your texture class, that's called by your texture manager (or whatever has a list of all your textures / buffers) (i.e. don't actually do any destructing).   ;Quote:Original post by LodeI don't have an Amiga myself, but people using AmigaOS4 are trying to compile my program. They discovered the following: on Windows and Linux, when you call glDeleteTextures after the GL context was destroyed, there is no problem. On AmigaOS4 however, it crashes if you call glDeleteTextures after the GL context was destroyed.Deleting textures after the context is destroyed? Since everything in OpenGL operates on the context, it makes no sense to delete textures after the context is destroyed. So whether it crashes or not isn't really relevant, since you shouldn't be doing it in the first place.Quote:Original post by LodeSo basically, I'm in a very messy situation: behaviour of SDL and OpenGL that is different on 3 different operating systems.This problem is combined with the fact that I can't correctly know for sure whether or not the GL context was deleted and recreated after doing SDL_SetVideoMode (see my other thread here).Basically, calling or not calling glDeleteTextures can result in white textures, a crash, or a memory leak depending on the combination of version of SDL and OS.So, in short, you're using a window manager where you have no well defined or consistent control over context management? If SDL (the windowing part of SDL, specifically) doesn't specify how it handles the context, which is almost its only function, I'd say you should look for another window manager. If you cannot know for sure how the context is managed, you will have problems managing your resources.Quote:Original post by LodeI also found at OpenGL.org itself, that a ctor in a class that does gentexture and a dtor that does deletetextures is NOT a good idea. http://www.opengl.org/wiki/Common_MistakesManaging resources in constructors and destructors not at all wrong in itself. What you have to be careful is about the scope of the objects, so the constructor is not called before the context is created, and/or the destructor is called after the context is destroyed. As long as the scope of the OpenGL resource is smaller than the scope of the context, there's nothing wrong with constructors and destructors.Quote:Original post by LodeSo in fact I'm trying to have a system that:-can create opengl textures-deletes opengl textures if needed to avoid memory leaks-does NOT crash when deleting textures-supports resizing of the window-depending on whether or not SDL_SetVideoMode destroyed the gl context, recreates the textures (and either has or has not to glDelete the ones from the previous context???)-works on linux, windows, mac, and amigaos4AmigaOS may be a limiting factor, but, as I wrote eariler, you need well defined and consistent behavior when it comes to context management. If SDL doesn't offer that, then I would recommend staying away from it.