ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
60.84k stars 10.28k forks source link

Segfault happened when I used a ListBox #5964

Closed uzername closed 1 year ago

uzername commented 1 year ago

Version/Branch of Dear ImGui:

Version: 1.89.2 VIP Branch: master. I used download ZIP

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_opengl3.cpp + imgui_impl_glfw.cpp Compiler: Visual Studio 2022 Operating System: Win10

My Issue/Question:

Segfault happening when using ListBox. I have such code for dear ImGui init:

// Setup Dear ImGui context
    const char* glsl_version = "#version 330";
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO();
    // Setup Platform/Renderer bindings
    ImGui_ImplGlfw_InitForOpenGL(myOcctWindow->getGlfwWindow(), true);
    ImGui_ImplOpenGL3_Init(glsl_version);
    // Setup Dear ImGui style
    ImGui::StyleColorsLight();

I have such code to be called in main cycle:

void processUI()
{
    // feed inputs to dear imgui, start new frame 
    ImGui_ImplOpenGL3_NewFrame();
    ImGui_ImplGlfw_NewFrame();
    ImGui::NewFrame();

    ImGui::Begin("File list");   
    ImGui::Button("Add File");
    ImGui::SameLine();
    ImGui::Button("Clear List");
    ImGui::ListBox("Files",&currentItem,listboxItems,1,10);
    ImGui::End();

    // Rendering
    ImGui::Render();
    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}

Variables are declared like this:

  //! UI related entries, list box mainly
  int currentItem;
  const char* listboxItems[1];
  int currentItemsCount;

When I try running my project, I get crash in ImGuiID ImHashStr(const char* data_p, size_t data_size, ImU32 seed). On line 1883 I get info that data was null. I guess, it happened because I should initialize my only element of listboxItems. Anyway, I'd like to inform that crash. I'd suggest, that if char* element is NULL then assume that it's an empty string, for display.

Screenshots/Video

image

Standalone, minimal, complete and verifiable example: See above

PathogenDavid commented 1 year ago

I'd suggest, that if char* element is NULL then assume that it's an empty string, for display.

I don't think Dear ImGui should be trying to guess what you meant here. If you want an empty string you should pass in an empty string.

It's pretty typical for C/C++ libraries to allow invalid inputs to simply go bang.

ocornut commented 1 year ago

It’s important and intended that we crash on NULL pointer strings. The exception are to accept empty string_view/range, which may force us to backtrack on a few things.

Note that ListBox() is an old wrapper to BeginListBox()/EndListBox(), and using those directly may facilitate things if you need to decide that you want null pointers to be displayed in a specific way.