Skip to content

Commit 53e5886

Browse files
authored
support a proper shutdown flow (#311)
1 parent 469427d commit 53e5886

File tree

16 files changed

+72
-28
lines changed

16 files changed

+72
-28
lines changed

examples/demo-app/demo_app.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,7 @@ int main(int argc, char** argv) {
880880
// }
881881

882882
std::cout << "!!!! shutdown time" << std::endl;
883+
polyscope::shutdown();
883884

884885
return 0;
885886
}

include/polyscope/messages.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ void exception(std::string message);
2727

2828
// Process any warnings that have accumulated, showing them to the user and clearing the queue.
2929
void showDelayedWarnings();
30+
void clearMessages();
3031
} // namespace polyscope

include/polyscope/polyscope.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void unshow();
6060
void frameTick();
6161

6262
// Do shutdown work and de-initialize Polyscope
63-
void shutdown();
63+
void shutdown(bool allowMidFrameShutdown=false);
6464

6565
// Returns true if the user has tried to exit the window at the OS level, e.g clicking the close button. Useful for
6666
// deciding when to exit your control loop when using frameTick()

include/polyscope/render/engine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ class Engine {
442442
virtual ~Engine();
443443

444444
// High-level control
445+
virtual void shutdown() {};
445446
virtual void checkError(bool fatal = false) = 0;
446447
void buildEngineGui();
447448

include/polyscope/render/mock_opengl/mock_gl_engine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ class MockGLEngine : public Engine {
325325

326326
// High-level control
327327
void initialize();
328+
virtual void shutdown() override;
328329
void checkError(bool fatal = false) override;
329330

330331
void swapDisplayBuffers() override;

include/polyscope/render/opengl/gl_engine_egl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class GLEngineEGL : public GLEngine {
3838

3939
// High-level control
4040
void initialize();
41+
virtual void shutdown() override;
4142
void swapDisplayBuffers() override;
4243
void checkError(bool fatal = false) override;
4344

include/polyscope/render/opengl/gl_engine_glfw.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class GLEngineGLFW : public GLEngine {
4848

4949
// High-level control
5050
void initialize();
51+
virtual void shutdown() override;
5152
void swapDisplayBuffers() override;
5253

5354
// === Windowing and framework things

include/polyscope/slice_plane.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class SlicePlane {
111111

112112
SlicePlane* addSceneSlicePlane(bool initiallyVisible = false);
113113
void removeLastSceneSlicePlane();
114+
void removeAllSlicePlanes();
114115
void buildSlicePlaneGUI();
115116

116117
// flag to open the slice plane menu after adding a slice plane

src/messages.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ void terminatingError(std::string message) {
235235
pushContext(func, false);
236236

237237
// Quit the program
238-
shutdown();
238+
shutdown(true);
239239
std::exit(-1);
240240
}
241241

@@ -281,4 +281,6 @@ void showDelayedWarnings() {
281281
}
282282
}
283283

284+
void clearMessages() { warningMessages.clear(); }
285+
284286
} // namespace polyscope

src/polyscope.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -934,14 +934,30 @@ bool windowRequestsClose() {
934934
return false;
935935
}
936936

937-
void shutdown() {
937+
void shutdown(bool allowMidFrameShutdown) {
938+
939+
if (!allowMidFrameShutdown && contextStack.size() > 1) {
940+
terminatingError("shutdown() was called mid-frame (e.g. in a per-frame callback, or UI element). This is not "
941+
"permitted, shutdown() may only be called when the main loop is not executing.");
942+
}
938943

939-
// TODO should we make an effort to destruct everything here?
940944
if (options::usePrefsFile) {
941945
writePrefsFile();
942946
}
943947

944-
render::engine->shutdownImGui();
948+
// Clear out all structures and other scene objects
949+
removeAllStructures();
950+
removeAllGroups();
951+
removeAllSlicePlanes();
952+
clearMessages();
953+
state::userCallback = nullptr;
954+
955+
// Shut down the render engine
956+
render::engine->shutdown();
957+
delete render::engine;
958+
render::engine = nullptr;
959+
state::backend = "";
960+
state::initialized = false;
945961
}
946962

947963
bool registerStructure(Structure* s, bool replaceIfPresent) {

0 commit comments

Comments
 (0)