Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DestroyContext should set GImDefaultContext #1007

Closed
sonoro1234 opened this issue Feb 3, 2017 · 17 comments
Closed

DestroyContext should set GImDefaultContext #1007

sonoro1234 opened this issue Feb 3, 2017 · 17 comments

Comments

@sonoro1234
Copy link

Hi,

actually DestroyContext is:

void ImGui::DestroyContext(ImGuiContext* ctx)
{
    void (*free_fn)(void*) = ctx->IO.MemFreeFn;
    ctx->~ImGuiContext();
    free_fn(ctx);
    if (GImGui == ctx)
        SetCurrentContext(NULL);
}

this makes imgui crash unless I set GImDefaultContext as the current context from the implementation.
perhaps it would be better to have:

void ImGui::DestroyContext(ImGuiContext* ctx)
{
    void (*free_fn)(void*) = ctx->IO.MemFreeFn;
    ctx->~ImGuiContext();
    free_fn(ctx);
    if (GImGui == ctx)
        SetCurrentContext(&GImDefaultContext);
}

or to avoid some extrange behaviour of free_fn changing ctx value

void ImGui::DestroyContext(ImGuiContext* ctx)
{
    void (*free_fn)(void*) = ctx->IO.MemFreeFn;
    ctx->~ImGuiContext();
    if (GImGui == ctx)
        SetCurrentContext(&GImDefaultContext);
    free_fn(ctx);
}
@ocornut
Copy link
Owner

ocornut commented Feb 4, 2017

Both very good points, looking into it.
I might also assert on ctx not being GImDefaultContext?

@ocornut
Copy link
Owner

ocornut commented Feb 4, 2017

  1. Hmm, sort of going backward on this. Setting to GImDefaultContext again may promote not understanding what's going on with contexts so it's debatable what's right. If you are creating contexts then maybe you don't need the default context at all? Or you could manage to retrieve its pointer?
    In the spirit of ImGui instance working out of the box without an initialisation, I think it be worth doing it but I'm not totally sure.

  2. I misread you on the second suggestion. free_fn() can't possibility change the ctx pointer it is a function.
    In fact calling free_fn() before is correct in case the free function rely on the current context (which in practice is unlikely as it's hard to track multiple allocators).

@sonoro1234
Copy link
Author

In my glfwgl3 implementation with contexts I get default context

https://github.com/sonoro1234/cimgui/blob/lua_build/extras/impl_glfw3/imgui_impl_glfw_gl3.h#L38

for being able to set it before destroying mine to avoid crashing (that's why i need it)

https://github.com/sonoro1234/cimgui/blob/lua_build/extras/impl_glfw3/imgui_impl_glfw_gl3.h#L52

I think it would be simpler if DestroyContext does this.

@ocornut
Copy link
Owner

ocornut commented Feb 4, 2017 via email

@sonoro1234
Copy link
Author

I am doing Shutdown afterwards, perhaps Shutdown is trying to access context.

@thedmd
Copy link
Contributor

thedmd commented Feb 4, 2017

Without looking closely at the issue, but basing on my experience. Crashing could be caused by GImDefaultFontAtlas which if initialized require valid context to be present during destruction. Otherwise ImVector it contains cannot access (valid) free function (see: ImGui::MemFree).

Bottom line is, if you destroy default context but keep using second one, when closing application crash is guaranteed.

To avoid such issues I modified my copy of ImGui to not create default context nor default atlas at all.
This way context setup is little more complicated than before, in return I will not have to deal with static destructors.

@sonoro1234
Copy link
Author

I prefer not to modify ImGui and only modify the implementation

@ocornut
Copy link
Owner

ocornut commented Feb 4, 2017

Note that you can #define GImGui in imconfig.h and among other things it will disable the default context.

I'm however interested in fixing that mess with the allocators. I think we should move the allocators outside of the context, into globals GImGuiMalloc GImGuifFree?

@sonoro1234
Copy link
Author

I dont mind having a default context. Just want to do destroycontext followed by shutdown. Solutions can be fixing allocators or just changing destroycontext to set default context.

@thedmd
Copy link
Contributor

thedmd commented Feb 4, 2017

Ability to override default malloc/free pair in imconfig.h looks like a good idea.

This will however not solve problem with finalisation order. GDefaultFontAtlas still has a ImVector and will be destructed after main(). By this time user allocator may be long gone.

I added options to disable creation of default context and global font atlas. I can prepare a PR with those changes if you like.

@sonoro1234
Copy link
Author

It is the same happenning in #992

@aiekick
Copy link
Contributor

aiekick commented Feb 20, 2017

do you have a fix for now ? please

@ocornut
Copy link
Owner

ocornut commented Feb 20, 2017

@aiekick you shouldn't be reliant on a fix since you can always set the context yourself afterward?
I will solve the malloc/free situation first! (again I'm completely overwhelmed with shipping my game now so all of this has been put aside for now)

@aiekick
Copy link
Contributor

aiekick commented Feb 20, 2017

i say that because, i failed to fix by the solution upper. but ok i search more. no problem. i understand :)

@aiekick
Copy link
Contributor

aiekick commented Feb 21, 2017

ok i just gain understanding of the principle of the imgui context. its ok, it work. thanks :)

@sonoro1234
Copy link
Author

@aiekick in #1007 (comment)
you can see how I solve it in my implementation: Save the default context in a variable and set it before destroying my context.

ocornut added a commit that referenced this issue Jan 20, 2018
…now setup with SetMemoryAllocators() and shared by all contexts. (#586, #992, #1007, #1558)
ocornut added a commit that referenced this issue Jan 21, 2018
…now setup with SetMemoryAllocators() and shared by all contexts. (#1565, #586, #992, #1007, #1558)
ocornut added a commit that referenced this issue Jan 21, 2018
- YOU NOW NEED TO CALL ImGui::CreateContext() AT THE BEGINNING OF YOUR APP, AND CALL ImGui::DestroyContext() AT THE END.
- removed Shutdown() function, as DestroyContext() serve this purpose.
- you may pass a ImFontAtlas* pointer to CreateContext() to share a font atlas between contexts. Otherwhise CreateContext() will create its own font atlas instance.
- removed allocator parameters from CreateContext(), they are now setup with SetAllocatorFunctions(), and shared by all contexts.
- removed the default global context and font atlas instance, which were confusing for users of DLL reloading and users of multiple contexts
(#1565, #586, #992, #1007, #1558)
ocornut added a commit that referenced this issue Jan 21, 2018
…ntext(), DestroyContext() in example code. Removed Shutdown() from binding code. (#1565, #586, #992, #1007, #1558)
@ocornut
Copy link
Owner

ocornut commented Feb 6, 2018

@sonoro1234 The issues discussed here should now be clarified and fixed by the changes discussed in #1565. Context creation are now explicit, and have a parameter to pass in a font atlas created by the user.
Read #1565 for details.

@ocornut ocornut closed this as completed Feb 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants