NogginBops / ImGui.NET_OpenTK_Sample

A sample project showing an ImGui (using ImGui.NET) renderer for OpenTK in C#
109 stars 26 forks source link

ImGuiController.Update() seems to be calling destructors on my objects #32

Closed Logabe closed 3 months ago

Logabe commented 3 months ago

I'm not entirely sure if this issue is my problem or your code's problem, but I'm putting it here in the hope that I can get some explanation of what's going on. I've been following the LearnOpenTK Tutorial up to tutorial 1.3 (Index buffers), and I've written helper classes to wrap around the concept of a mesh and shader. These classes implement IDisposable, but have destructors that will warn if the object hasn't properly been disposed yet. Before using the ImGuiController, there weren't any problems. After integrating the controller based off of the Window.cs file seems to be calling the destructors. For some strange reason, putting controller.Update(this, (float)e.Time); as used in the example program will cause the destructor to be called once on:

NogginBops commented 3 months ago

Do you keep references to these objects? To me this sounds like you are creating objects that you don't retain a reference to.

Logabe commented 3 months ago

Yes, I keep the objects stored in the main Window class. They still draw every frame.

NogginBops commented 3 months ago

Then without seeing some of your code I can't really say anything. My code should not be able to invoke the dispose method of your objects.

Logabe commented 3 months ago

I've uploaded an archive containing the project - I wasn't sure what was important or not, but it isn't massive since this is still very much a testing project. I hope this can help. Project.zip Again, there's a good change that it's entirely my fault, but I couldn't find any reason why that would be. Thanks again, Logabe

NogginBops commented 3 months ago

MainState.cs: image Model.cs: image

Here you are creating objects in the constructor of the object that you then override. This means that the original objects you create are going to be garbage collected without having been disposed, causing the printout. The reason this didn't happen before is likely because the code before never did anything that triggered a garbage collection.

The solution is to not allocate objects in the constructor and only do it in OnEnter.