Skip to content

Commit

Permalink
Fix crash when quitting mid-render
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeeter committed Oct 23, 2017
1 parent 9d57a23 commit a7ffb99
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
9 changes: 9 additions & 0 deletions gui/include/gui/shape.hpp
Expand Up @@ -112,6 +112,15 @@ class Shape : public QObject, QOpenGLFunctions
*/
void setHover(bool h);

/*
* Destroys all OpenGL objects associated with this shape
*
* This must be called when the context is current, and must
* be called before the context is destroyed (otherwise, the Shape
* destructor may try to use a now-destroyed context)
*/
void freeGL();

signals:
void gotMesh();
void redraw();
Expand Down
9 changes: 9 additions & 0 deletions gui/include/gui/view.hpp
Expand Up @@ -46,6 +46,15 @@ class View : public QOpenGLWidget, QOpenGLFunctions
*/
void cancelShapes();

/*
* In the destructor, free the OpenGL data associated with
* all children Shapes (because there could be shapes that
* aren't explicitly stored in the object, which will be freed
* by the QObject destructor, which runs after the OpenGL context
* is gone).
*/
~View();

public slots:
void setShapes(QList<Shape*> shapes);
void openSettings();
Expand Down
12 changes: 12 additions & 0 deletions gui/src/shape.cpp
Expand Up @@ -303,6 +303,18 @@ void Shape::onFutureFinished()
}
}

void Shape::freeGL()
{
if (gl_ready)
{
vao.destroy();
vert_vbo.destroy();
tri_vbo.destroy();

gl_ready = false;
}
}

////////////////////////////////////////////////////////////////////////////////
// This function is called in a separate thread:
Kernel::Mesh* Shape::renderMesh(QPair<Settings, int> s)
Expand Down
10 changes: 10 additions & 0 deletions gui/src/view.cpp
Expand Up @@ -45,6 +45,16 @@ View::View(QWidget* parent)
connect(&pick_timer, &QTimer::timeout, this, &View::redrawPicker);
}

View::~View()
{
makeCurrent();
for (auto s : findChildren<Shape*>())
{
s->freeGL();
}
doneCurrent();
}

void View::setShapes(QList<Shape*> new_shapes)
{
// Pack tree IDs into a pair of sets for fast checking
Expand Down

0 comments on commit a7ffb99

Please sign in to comment.