Skip to content

Commit

Permalink
Shutdown safely and correctly on Apple
Browse files Browse the repository at this point in the history
Metal and OSX require `ImGui_Impl*_GetBackendData() != nullptr` during
shutdown anyway so this is not a breaking change. Actually, before this
PR there were potential `nullptr` derefs in the respective
`ImGui_Impl*_DestroyBackendData()`s. This PR both prevents this and also
resets `io.BackendRendererUserData`, `io.BackendRendererName` and
`io.BackendFlags`.
  • Loading branch information
GereonV committed Apr 15, 2023
1 parent 078aa92 commit 7d40dd4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions backends/imgui_impl_metal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ bool ImGui_ImplMetal_Init(id<MTLDevice> device)

void ImGui_ImplMetal_Shutdown()
{
ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO();
io.BackendRendererUserData = nullptr;
io.BackendRendererName = nullptr;
io.BackendFlags &= ~ImGuiBackendFlags_RendererHasVtxOffset;
ImGui_ImplMetal_DestroyDeviceObjects();
ImGui_ImplMetal_DestroyBackendData();
}
Expand Down
7 changes: 7 additions & 0 deletions backends/imgui_impl_osx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,19 @@ bool ImGui_ImplOSX_Init(NSView* view)
void ImGui_ImplOSX_Shutdown()
{
ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO();

bd->Observer = nullptr;
if (bd->Monitor != nullptr)
{
[NSEvent removeMonitor:bd->Monitor];
bd->Monitor = nullptr;
}

io.BackendPlatformName = nullptr;
io.BackendPlatformUserData = nullptr;
io.BackendFlags &= ~ImGuiBackendFlags_HasMouseCursors;
ImGui_ImplOSX_DestroyBackendData();
}

Expand Down

0 comments on commit 7d40dd4

Please sign in to comment.