ImGuiNET / ImGui.NET

An ImGui wrapper for .NET.
MIT License
1.85k stars 300 forks source link

ImGui.NewFrame() crashes. #165

Open MatijaBrown opened 4 years ago

MatijaBrown commented 4 years ago

I know there are allot of issues to do with AccesViolationExceptions here, but I couldn't find anything that helped my situation. I just want to show a debug window, just to test ImGui - here is my code: `

        float time = (float)Silk.NET.GLFW.Glfw.GetApi().GetTime();
        ImGuiIOPtr io = ImGui.GetIO();

        io.DeltaTime = m_time > 0.0f ? (time - m_time) : (1.0f / 60f);
        m_time = time;

        io.DisplaySize = new Vector2(1280, 720);

        ImGui.NewFrame();

        bool show = true;
        ImGui.ShowDemoWindow(ref show);

        ImGui.Render();`

My code worked up till now, and also this code. But when I reach the ImGui.NewFrame() call, it crashes with a AccessViolationError - Attempted to write to protected memory.

Any sugestions what I did wrong? Thanks in advance and have a good time laughing about my (probably) very obvious mistake.

joebobmiles commented 4 years ago

I'm not sure this is a very obvious mistake. Just ran into the same issue trying to integrate ImGui with a UWP application. I attempted to drill down into the offending ImGui.NewFrame() call, but was only greeted with an AccessViolationException. The ImGui and ImGui.NET examples seem to suggest that it should work.

Only question I have for devs is if a project integrating ImGui.NET requires the cimgui DLL to be in the project files. I know the README states that it is included in the NuGet package, but it's the only thing I can think of that would lead to a possible issue.

joebobmiles commented 4 years ago

A couple of updates.

Turns out the answer to my question is no - cimgui.dll is being integrated as expected.

I've also coaxed some information from the Visual Studio debugger that get's closer to the source of my error. After setting up my Visual Studio to debug both managed and native code, I got the following exception and stack trace:

Exception thrown at 0x13CBC92A (cimgui.dll) in STI Processor UI Prototype.exe: 0xC0000005: Access violation reading location 0x0000002C.
    cimgui.dll!13cbc92a()   Unknown
    cimgui.dll![Frames below may be incorrect and/or missing, no symbols loaded for cimgui.dll] Unknown
    cimgui.dll!13cb6c0e()   Unknown
    [Managed to Native Transition]  
    ImGui.NET.dll!ImGuiNET.ImGui.NewFrame() Unknown
>   STI Processor UI Prototype.exe!STI_Processor_UI_Prototype.MainPage.DrawPrimaryChart(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs e) Line 107   C#

Turns out that the AccessViolationException is being caused by cimgui. (And possibly ImGui?)

@MatijaBrown Are you using Visual Studio for your project? If so, you can enable mixed mode debugging and check to see if you are getting the same exception and trace.

Unfortunately the cimgui.dll that ships with the ImGui.NET NuGet package doesn't have any debugger symbols, so I can't drill down any further till I figure out how to swap the shipped DLL with a debug DLL.

MatijaBrown commented 4 years ago

I do use vs. I'll try the debugger. Thanks for the reply, sorry i couldn't answere earlier.

dominota commented 4 years ago

@JoeBobMiles Do you init a font?

joebobmiles commented 4 years ago

Yes. Unfortunately I can't post the original code since the project I mentioned moved on with a different solution. As I wrote in #178 , it would be invaluable to have a debug build of cimgui.dll for debugging these issues without having to resort to blind troubleshooting.

dominota commented 4 years ago

Unfortunately, The core of Slik.Net is OpenGL. I use SharpDX with DirectX so i can easy to integrate ImGui.Net.DirectX into my project with just some APIs

joebobmiles commented 4 years ago

Unfortunately, The core of Slik.Net is OpenGL. I use SharpDX with DirectX so i can easy to integrate ImGui.Net.DirectX into my project with just some APIs

I'm not sure how that helps Matija and I solve our problems with ImGui.NewFrame() crashing.

stephen-wood83 commented 4 years ago

Are you following the Cherno? I am trying to achieve the same thing, I've managed to get that working by using Veldrid but the Imgui layer is in a separate window and now I'm stuck. Has anyone manage to have the Imgui overlay appear within the glfw/game window?

MatijaBrown commented 4 years ago

I do (sort of). I just took some code that should have worked and put it in, because I needed some quick testing UI.

stephen-wood83 commented 4 years ago

Do you have any code you can share?

bootzin commented 3 years ago

If it helps, I have some code at this repo that is my take on The Cherno's engine It's not up to date, but I intend on getting back to it in the near future Nevertheless, I did everything up to batch rendering, so it may help you find your problem (check the batch-render branch)