0
votes

I have a class named Mesh which wraps and OpenGL object, with VBO, texture, etc. This class inherits from QObject and its parent is set to my QGLWidget. So as soon I close the widget it will get deleted by QObject system.

But I have a problem because this mesh class has a destructor that does some OpenGL calls to cleanup. When QObject child destructor kicks in, the OpenGL context is already destroyed and I get a segfault.

How I can make sure that my ~Mesh() destructor will get called before QGLWidget context gets deleted?

1
I'd suggest checking out QOpenGLContext and managing it yourself. - Ben

1 Answers

1
votes

You could delete the Mesh object explicitly in the destructor of your QGLWidget derived class, because everything you put in there would be executed before ~QGLWidget() itself (and before ~QObject() which is responsible for the deletion of child objects).

Or you can make the object a non-pointer member, or wrap it in a smart pointer to achieve the same result.