Skip to content

Commit 1a309ed

Browse files
committed
Hold on Window std::shared_ptr longer to avoid a crash at shudown
The crash is caused by acquiring content of the shared_ptr after it was released. Added two shared_ptrs, one for ImGuiWMInit/Shudtown and the other for ImGuiBackendInit/Shutdown. this way we are sure it's not used anymore by anyone.
1 parent a53366f commit 1a309ed

2 files changed

Lines changed: 20 additions & 19 deletions

File tree

include/fast/Fast3dGui.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,5 +168,8 @@ class Fast3dGui : public Ship::Gui {
168168
int16_t GetIntegerScaleFactor();
169169

170170
std::unordered_map<std::string, Ship::GuiTextureMetadata> mGuiTextures; ///< Cached GPU texture registry.
171+
172+
std::shared_ptr<Ship::Window> mWindow;
173+
std::shared_ptr<Ship::Window> mBackendWindow;
171174
};
172175
} // namespace Fast

src/fast/Fast3dGui.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ bool Fast3dGui::SupportsViewports() {
6666
}
6767

6868
void Fast3dGui::HandleWindowEvents(Fast::WindowEvent event) {
69-
auto window = Ship::Context::GetInstance()->GetWindow();
70-
switch (window->GetWindowBackend()) {
69+
switch (mWindow->GetWindowBackend()) {
7170
case WindowBackend::FAST3D_SDL_OPENGL:
7271
case WindowBackend::FAST3D_SDL_METAL:
7372
ImGui_ImplSDL2_ProcessEvent(static_cast<const SDL_Event*>(event.Sdl.Event));
@@ -87,10 +86,11 @@ void Fast3dGui::HandleWindowEvents(Fast::WindowEvent event) {
8786
}
8887

8988
void Fast3dGui::ImGuiWMInit() {
90-
auto window = Ship::Context::GetInstance()->GetWindow();
91-
mInterpreter = std::dynamic_pointer_cast<Fast3dWindow>(window)->GetInterpreterWeak();
89+
mWindow = Ship::Context::GetInstance()->GetWindow();
90+
if (auto fast3dWindow = std::dynamic_pointer_cast<Fast3dWindow>(mWindow))
91+
mInterpreter = fast3dWindow->GetInterpreterWeak();
9292

93-
switch (window->GetWindowBackend()) {
93+
switch (mWindow->GetWindowBackend()) {
9494
case WindowBackend::FAST3D_SDL_OPENGL:
9595
SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "1");
9696
if (Ship::Context::GetInstance()->GetConsoleVariables()->GetInteger(CVAR_ALLOW_BACKGROUND_INPUTS, 1)) {
@@ -121,8 +121,7 @@ void Fast3dGui::ImGuiWMInit() {
121121
}
122122

123123
void Fast3dGui::ImGuiWMShutdown() {
124-
auto window = Ship::Context::GetInstance()->GetWindow();
125-
switch (window->GetWindowBackend()) {
124+
switch (mWindow->GetWindowBackend()) {
126125
#ifdef ENABLE_OPENGL
127126
case WindowBackend::FAST3D_SDL_OPENGL:
128127
ImGui_ImplSDL2_Shutdown();
@@ -141,11 +140,13 @@ void Fast3dGui::ImGuiWMShutdown() {
141140
default:
142141
break;
143142
}
143+
144+
mWindow = nullptr;
144145
}
145146

146147
void Fast3dGui::ImGuiBackendInit() {
147-
auto window = Ship::Context::GetInstance()->GetWindow();
148-
switch (window->GetWindowBackend()) {
148+
mBackendWindow = Ship::Context::GetInstance()->GetWindow();
149+
switch (mBackendWindow->GetWindowBackend()) {
149150
#ifdef ENABLE_OPENGL
150151
case WindowBackend::FAST3D_SDL_OPENGL:
151152
#ifdef __APPLE__
@@ -178,8 +179,7 @@ void Fast3dGui::ImGuiBackendInit() {
178179
}
179180

180181
void Fast3dGui::ImGuiBackendShutdown() {
181-
auto window = Ship::Context::GetInstance()->GetWindow();
182-
switch (window->GetWindowBackend()) {
182+
switch (mBackendWindow->GetWindowBackend()) {
183183
#ifdef ENABLE_OPENGL
184184
case WindowBackend::FAST3D_SDL_OPENGL:
185185
ImGui_ImplOpenGL3_Shutdown();
@@ -198,11 +198,12 @@ void Fast3dGui::ImGuiBackendShutdown() {
198198
default:
199199
break;
200200
}
201+
202+
mBackendWindow = nullptr;
201203
}
202204

203205
void Fast3dGui::ImGuiBackendNewFrame() {
204-
auto window = Ship::Context::GetInstance()->GetWindow();
205-
switch (window->GetWindowBackend()) {
206+
switch (mBackendWindow->GetWindowBackend()) {
206207
#ifdef ENABLE_OPENGL
207208
case WindowBackend::FAST3D_SDL_OPENGL:
208209
ImGui_ImplOpenGL3_NewFrame();
@@ -228,8 +229,7 @@ void Fast3dGui::ImGuiBackendNewFrame() {
228229
}
229230

230231
void Fast3dGui::ImGuiWMNewFrame() {
231-
auto window = Ship::Context::GetInstance()->GetWindow();
232-
switch (window->GetWindowBackend()) {
232+
switch (mWindow->GetWindowBackend()) {
233233
case WindowBackend::FAST3D_SDL_OPENGL:
234234
case WindowBackend::FAST3D_SDL_METAL:
235235
ImGui_ImplSDL2_NewFrame();
@@ -257,8 +257,7 @@ void Fast3dGui::RefreshImGuiGamepads() {
257257
}
258258

259259
void Fast3dGui::ImGuiRenderDrawData(ImDrawData* data) {
260-
auto window = Ship::Context::GetInstance()->GetWindow();
261-
switch (window->GetWindowBackend()) {
260+
switch (mWindow->GetWindowBackend()) {
262261
#ifdef ENABLE_OPENGL
263262
case WindowBackend::FAST3D_SDL_OPENGL:
264263
ImGui_ImplOpenGL3_RenderDrawData(data);
@@ -288,9 +287,8 @@ void Fast3dGui::DrawFloatingWindows() {
288287
return;
289288
}
290289

291-
auto window = Ship::Context::GetInstance()->GetWindow();
292290
// OpenGL requires extra platform handling for the GL context
293-
if (window->GetWindowBackend() == WindowBackend::FAST3D_SDL_OPENGL && mImpl.Opengl.Context != nullptr) {
291+
if (mWindow->GetWindowBackend() == WindowBackend::FAST3D_SDL_OPENGL && mImpl.Opengl.Context != nullptr) {
294292
// Backup window and context before calling RenderPlatformWindowsDefault
295293
SDL_Window* backupCurrentWindow = SDL_GL_GetCurrentWindow();
296294
SDL_GLContext backupCurrentContext = SDL_GL_GetCurrentContext();

0 commit comments

Comments
 (0)