Skip to content

Commit d6da9fa

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 3b19620 commit d6da9fa

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
@@ -166,5 +166,8 @@ class Fast3dGui : public Ship::Gui {
166166
int16_t GetIntegerScaleFactor();
167167

168168
std::unordered_map<std::string, Ship::GuiTextureMetadata> mGuiTextures; ///< Cached GPU texture registry.
169+
170+
std::shared_ptr<Ship::Window> mWindow;
171+
std::shared_ptr<Ship::Window> mBackendWindow;
169172
};
170173
} // 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)) {
@@ -118,8 +118,7 @@ void Fast3dGui::ImGuiWMInit() {
118118
}
119119

120120
void Fast3dGui::ImGuiWMShutdown() {
121-
auto window = Ship::Context::GetInstance()->GetWindow();
122-
switch (window->GetWindowBackend()) {
121+
switch (mWindow->GetWindowBackend()) {
123122
#ifdef ENABLE_OPENGL
124123
case WindowBackend::FAST3D_SDL_OPENGL:
125124
ImGui_ImplSDL2_Shutdown();
@@ -138,11 +137,13 @@ void Fast3dGui::ImGuiWMShutdown() {
138137
default:
139138
break;
140139
}
140+
141+
mWindow = nullptr;
141142
}
142143

143144
void Fast3dGui::ImGuiBackendInit() {
144-
auto window = Ship::Context::GetInstance()->GetWindow();
145-
switch (window->GetWindowBackend()) {
145+
mBackendWindow = Ship::Context::GetInstance()->GetWindow();
146+
switch (mBackendWindow->GetWindowBackend()) {
146147
#ifdef ENABLE_OPENGL
147148
case WindowBackend::FAST3D_SDL_OPENGL:
148149
#ifdef __APPLE__
@@ -175,8 +176,7 @@ void Fast3dGui::ImGuiBackendInit() {
175176
}
176177

177178
void Fast3dGui::ImGuiBackendShutdown() {
178-
auto window = Ship::Context::GetInstance()->GetWindow();
179-
switch (window->GetWindowBackend()) {
179+
switch (mBackendWindow->GetWindowBackend()) {
180180
#ifdef ENABLE_OPENGL
181181
case WindowBackend::FAST3D_SDL_OPENGL:
182182
ImGui_ImplOpenGL3_Shutdown();
@@ -195,11 +195,12 @@ void Fast3dGui::ImGuiBackendShutdown() {
195195
default:
196196
break;
197197
}
198+
199+
mBackendWindow = nullptr;
198200
}
199201

200202
void Fast3dGui::ImGuiBackendNewFrame() {
201-
auto window = Ship::Context::GetInstance()->GetWindow();
202-
switch (window->GetWindowBackend()) {
203+
switch (mBackendWindow->GetWindowBackend()) {
203204
#ifdef ENABLE_OPENGL
204205
case WindowBackend::FAST3D_SDL_OPENGL:
205206
ImGui_ImplOpenGL3_NewFrame();
@@ -225,8 +226,7 @@ void Fast3dGui::ImGuiBackendNewFrame() {
225226
}
226227

227228
void Fast3dGui::ImGuiWMNewFrame() {
228-
auto window = Ship::Context::GetInstance()->GetWindow();
229-
switch (window->GetWindowBackend()) {
229+
switch (mWindow->GetWindowBackend()) {
230230
case WindowBackend::FAST3D_SDL_OPENGL:
231231
case WindowBackend::FAST3D_SDL_METAL:
232232
ImGui_ImplSDL2_NewFrame();
@@ -242,8 +242,7 @@ void Fast3dGui::ImGuiWMNewFrame() {
242242
}
243243

244244
void Fast3dGui::ImGuiRenderDrawData(ImDrawData* data) {
245-
auto window = Ship::Context::GetInstance()->GetWindow();
246-
switch (window->GetWindowBackend()) {
245+
switch (mWindow->GetWindowBackend()) {
247246
#ifdef ENABLE_OPENGL
248247
case WindowBackend::FAST3D_SDL_OPENGL:
249248
ImGui_ImplOpenGL3_RenderDrawData(data);
@@ -273,9 +272,8 @@ void Fast3dGui::DrawFloatingWindows() {
273272
return;
274273
}
275274

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

0 commit comments

Comments
 (0)