Nelarius / imnodes

A small, dependency-free node editor for dear imgui
MIT License
2.04k stars 249 forks source link

Multiple Contexts don't work as expected #143

Closed Thalhammer closed 2 years ago

Thalhammer commented 2 years ago

Describe the bug Maybe I am not using it correctly, but it looks like creating multiple contexts is broken. From what I found the reason for this is that void Initialize(ImNodesContext* context) sets GImNodes->EditorCtx to GImNodes->DefaultEditorCtx even if the newly created context is not the current global context. This is causing a crash once you attempt to use the newly created context, because context->EditorCtx is null. https://github.com/Nelarius/imnodes/blob/master/imnodes.cpp#L1633 The fix for this should be simple:

How To Reproduce

auto global = ImNodes::CreateContext(); // Creates global context
auto ctx = ImNodes::CreateContext(); // New context
ImNodes::SetCurrentContext(ctx);
// Do any operation here => Crash as soon as it touches GImNodes->EditorCtx

Expected behavior global and ctx represent two distinct contexts, each of which can be used for rendering and does not influence each other or leak memory.

Desktop:

Thalhammer commented 2 years ago

Found out you're supposed to use CreateEditorContext for this use case, however I still think that given the functions for multiple contexts exists it should work correctly.

Nelarius commented 2 years ago

Hi @Thalhammer ! Glad you were able to figure it out.

You raised an interesting issue. There's two kinds of state behind the scenes:

I suppose there is no reason why we couldn't just have a single ImNodesContext which would contain everything we need. It might be easier for someone who is trying to use the library solely based on reading imnodes.h.

I will keep this open issue open, as this is something to consider before making the API final.