TASEmulators / TASEmulators/BizHawk
ImGui draw list can grow uncontrollably while BizHawk is minimized
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 2.8k
- Forks
- 468
- PR merge metrics
- No merged PRs in 30d
Description
Summary
I had some crashes while leaving BizHawk running in the background with a Lua script enabled. While investigating these I noticed that BizHawk RAM usage was growing to multiple gigabytes, which wasn't released even after closing the Lua console and the rom.
As far as I can tell (having no clue about any of the code involved), while the main window is minimized, gui draw commands keep getting added to the command buffer but not executed until the window is restored. The memory used by this buffer is never released either.
Repro
- Load the test script
- Unpause and optionally unthrottle
- Minimize main window
- Wait for some time (a minute or so, depends on FPS)
- Restore main window
while true do
for i = 1, 255 do
gui.drawLine(0, i, 100, i, 0xFF0000 | i << 24)
end
emu.frameadvance();
end
I also added debug output here:
--- a/src/BizHawk.Bizware.Graphics/Renderers/ImGui2DRenderer.cs
+++ b/src/BizHawk.Bizware.Graphics/Renderers/ImGui2DRenderer.cs
@@ -137,6 +137,7 @@ namespace BizHawk.Bizware.Graphics
private void ResetDrawList()
{
+ System.Diagnostics.Trace.WriteLine($"ResetDrawList() Size={_imGuiDrawList.CmdBuffer.Size} Capacity={_imGuiDrawList.CmdBuffer.Capacity}");^M
ClearGCHandles();
_imGuiDrawList._ResetForNewFrame();
_imGuiDrawList.Flags |= ImDrawListFlags.AllowVtxOffset;
Output
While minimized, BizHawk memory usage starts to increase (in steps, whenever the buffer is resized presumably).
When unminimized after some time, BizHawk will likely freeze for a moment and the drawing should appear solid red for a frame as countless translucent lines are drawn on top of each other.
...
ResetDrawList() Size=256 Capacity=681
ResetDrawList() Size=256 Capacity=681
ResetDrawList() Size=256 Capacity=681
(nothing while minimized)
ResetDrawList() Size=946816 Capacity=1005310
ResetDrawList() Size=256 Capacity=1005310
ResetDrawList() Size=256 Capacity=1005310
ResetDrawList() Size=256 Capacity=1005310
...
If BizHawk is not restored before memory is exhausted, an access violation will occur.
System.AccessViolationException
[Managed to Native Transition]
> BizHawk.Bizware.Graphics.dll!BizHawk.Bizware.Graphics.ImGui2DRenderer.DrawLineInternal(System.Drawing.Color color, int x1, int y1, int x2, int y2) Line 564 C#
BizHawk.Bizware.Graphics.dll!BizHawk.Bizware.Graphics.ImGui2DRenderer.DrawLine(System.Drawing.Color color, int x1, int y1, int x2, int y2) Line 570 C#
BizHawk.Client.Common.dll!BizHawk.Client.Common.GuiApi.DrawLine(int x1, int y1, int x2, int y2, System.Drawing.Color? color, BizHawk.Client.Common.DisplaySurfaceID? surfaceID) Line 351 C#
BizHawk.Client.Common.dll!BizHawk.Client.Common.GuiLuaLibrary.DrawLine(int x1, int y1, int x2, int y2, object color, string surfaceName) Line 195 C#
[Native to Managed Transition]
[Managed to Native Transition]
NLua.dll!NLua.Method.LuaMethodWrapper.CallInvoke(NLua.Native.LuaState luaState, System.Reflection.MethodBase method, object targetObject) Unknown
NLua.dll!NLua.Method.LuaMethodWrapper.Call(System.IntPtr state) Unknown
NLua.dll!NLua.MetaFunctions.RunFunctionDelegate(System.IntPtr luaState) Unknown
[Native to Managed Transition]
[Managed to Native Transition]
NLua.dll!NLua.Native.LuaNativeMethods.Resume(System.IntPtr luaState, System.IntPtr from, int nargs) Unknown
NLua.dll!NLua.LuaThread.Resume() Unknown
EmuHawk.exe!BizHawk.Client.EmuHawk.LuaLibraries.ResumeScript(BizHawk.Client.Common.LuaFile lf) Line 373 C#
EmuHawk.exe!BizHawk.Client.EmuHawk.LuaConsole.ResumeScripts.AnonymousMethod__1() Line 632 C#
BizHawk.Client.Common.dll!BizHawk.Client.Common.LuaSandbox.Sandbox(System.Action callback, System.Action exceptionCallback) Line 56 C#
EmuHawk.exe!BizHawk.Client.EmuHawk.LuaConsole.ResumeScripts(bool includeFrameWaiters) Line 627 C#
EmuHawk.exe!BizHawk.Client.EmuHawk.MainForm.ProgramRunLoop() Line 977 C#
EmuHawk.exe!BizHawk.Client.EmuHawk.Program.SubMain(string[] args) Line 377 C#
EmuHawk.exe!BizHawk.Client.EmuHawk.Program.Main(string[] args) Line 86 C#
Host env.
- BizHawk 2.11.1? Dev, Win10 Pro 22H2; Intel/NVIDIA
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/BizHawk.Bizware.Graphics/Renderers/ImGui2DRenderer.cs, especially ResetDrawList, DrawLineInternal, and the draw-list reset path. Reproduce the issue with the Lua script while minimizing and monitor the reported draw-list size and process memory. Done means minimized windows no longer accumulate unbounded commands or crash when restored.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- desktop-dev, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100