Imgui: Accessing GetIO() before CreateContext() crashes

Created on 10 Jul 2018  路  5Comments  路  Source: ocornut/imgui

Hi, I've updated my dear Imgui version to 1.62 from 1.50 and after I set up everything working, my program crashed and I figured io.IniFilename = NULL causes that.

I've read the documentation on imgui.cpp but I couldn't see anything. Is there anything new about disabling ini setting? Thanks

Most helpful comment

In a nutshell, I have to init io before everytime I'm gonna use it in a function. I can't use it with only one init.

Since 1.60 you can only access ImGui::GetIO() once you have created a Context, this is what the assert in GetIO() should have been telling you:

ImGuiIO& ImGui::GetIO()
{
    IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() or ImGui::SetCurrentContext()?");
    return GImGui->IO;
}

If you want a global ImGuiIO& io when you can add a global ImGuiContext* ctx = ImGui::CreateContext() before the call to IO but make sure it only exists in 1 compilation unit (not in a header file).

All 5 comments

If there is a crash please provide a callstack and info about the crash. Thanks!

Solved with io.IniFilename = ""; instead of NULL

I don't understand how io.IniFilename = NULL; could possibly make imgui crash and I cannot reproduce it, so would appreciate details from you. If there is an issue on imgui side we should fix it. Thanks.

I've figured out, issue wasn't the NULL, the issue is; when I was using the old version, I was setting the io outside of any function.
screenshot_1 like this and this was the issue but before I figured this was the issue I've move it upside of my io.IniFilename code and then changed NULL to blank quotes and finally I thought issue was solved with quotes but it wasn't.

In a nutshell, I have to init io before everytime I'm gonna use it in a function. I can't use it with only one init.

In a nutshell, I have to init io before everytime I'm gonna use it in a function. I can't use it with only one init.

Since 1.60 you can only access ImGui::GetIO() once you have created a Context, this is what the assert in GetIO() should have been telling you:

ImGuiIO& ImGui::GetIO()
{
    IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() or ImGui::SetCurrentContext()?");
    return GImGui->IO;
}

If you want a global ImGuiIO& io when you can add a global ImGuiContext* ctx = ImGui::CreateContext() before the call to IO but make sure it only exists in 1 compilation unit (not in a header file).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SlNPacifist picture SlNPacifist  路  3Comments

KaungZawHtet picture KaungZawHtet  路  3Comments

inflex picture inflex  路  3Comments

bogdaNNNN1 picture bogdaNNNN1  路  3Comments

bizehao picture bizehao  路  3Comments