Skip to content

SIGSEGV when calling OpenGL function from DLL #8406

@Cthulhoni

Description

@Cthulhoni

Version/Branch of Dear ImGui:

master

Back-ends:

imgui_impl_sdl2.cpp + imgui_impl_opengl3.cpp

Compiler, OS:

Linux + clang 19.1.7

Full config/build information:

Dear ImGui 1.91.9 WIP (19184)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=201703
define: __linux__
define: __GNUC__=4
define: __clang_version__=19.1.7 
--------------------------------
io.BackendPlatformName: imgui_impl_sdl2
io.BackendRendererName: imgui_impl_opengl3
io.ConfigFlags: 0x00000003
 NavEnableKeyboard
 NavEnableGamepad
io.ConfigNavCaptureKeyboard
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x0000000E
 HasMouseCursors
 HasSetMousePos
 RendererHasVtxOffset
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,64
io.DisplaySize: 1280.00,720.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 0.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Details:

I am working on a small game engine using SDL2/OpenGL that also does hot reloading (function for rendering is loaded from a shared library).

I want to initialize ImGui in my main function and render the windows in the render function from a shared lib.
I've used Get/SetCurrentContext to have the same ImGuiContext in both the main program and the library

Init in main:

  IMGUI_CHECKVERSION();
  imguiContext_p = ImGui::CreateContext();
  ImGuiIO& io = ImGui::GetIO();
  (void)io;
  io.ConfigFlags |=
      ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
   io.ConfigFlags |=
       ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls

  // Setup Dear ImGui style
  ImGui::StyleColorsDark();
  // ImGui::StyleColorsLight();

  // Setup Platform/Renderer backends
  ImGui_ImplSDL2_InitForOpenGL(window_p, glContext);
  ImGui_ImplOpenGL3_Init(g_glslVersion);
  ImGui::GetAllocatorFunctions(&imGuiAllocFunc,
                               &imGuiFreeFunc,
                               &imGuiUserData_p);

Usage in shared lib:

  if (imGuiInit)
  {
    ImGui::SetCurrentContext(imguiContext_p);
    ImGui::SetAllocatorFunctions(imGuiAllocFunc,
                                 imGuiFreeFunc,
                                 imGuiUserData_p);
    //ImGui_ImplOpenGL3_dllInit();
    imGuiInit = true;

  }

  ImGui_ImplOpenGL3_NewFrame();
  ImGui_ImplSDL2_NewFrame();
  ImGui::NewFrame();

  if (showDemoWindow)
    ImGui::ShowDemoWindow(&showDemoWindow);

  ImGui::Render();

This crashes when trying to call the first OpenGL function on the shared lib side (glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);)
Backtrace:

Thread 1 "example_sdl2_op" received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x00007fffe019d85e in ImGui_ImplOpenGL3_CreateDeviceObjects () at ../../backends/imgui_impl_opengl3.cpp:765
#2  0x00007fffe019c297 in ImGui_ImplOpenGL3_NewFrame () at ../../backends/imgui_impl_opengl3.cpp:415
#3  0x00007fffe00a6db5 in render (context_p=0x555555a791c0, window_p=0x555555896080) at shared.cpp:24
#4  0x0000555555559af9 in main () at main.cpp:169

When looking at it in GDB it seems like OpenGL functions pointers are not NULL in imgl3wProcs on shared lib side but calling it still results in a crash.

I've added ImGui_ImplOpenGL3_dllInit() function that only calls imgl3wInit() and calling it once on the shared lib side solves the crashing problem but I feel like there is something wrong in my setup/usage of ImGui

I've included a link to a PR that includes changed example_sdl2_opengl3 as well as ImGui_ImplOpenGL3_dllInit() that shows this issue

Is there a way to solve this issue without having to initialize OpenGl twice?

Thanks in advance.

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

Uncomment line 20 in shared.cpp to make it not crash

Cthulhoni#1

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions