ImGui_ImplOpenGL3_init() returns a segfault with GLEW #4279

Closed
opened 2021-06-29 01:48:31 +00:00 by wbehrens-on-gh · 13 comments
wbehrens-on-gh commented 2021-06-29 01:48:31 +00:00 (Migrated from github.com)

Version/Branch of Dear ImGui:
imgui/1.83
glfw/3.3.4
glew/2.2.0
all pulled from Conan

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_glfw.cpp + imgui_impl_opengl3.cpp
Compiler: clang(++)
Operating System: archlinux

My Issue/Question:
ImGui_ImplOpenGL3_init(); gives a segmentation fault when called

Main.cpp

    const char* glsl_version = "#version 130";
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    glfwInit();
    glewInit();
    IMGUI_CHECKVERSION();
    
    GLFWwindow* win = glfwCreateWindow(500, 400, "window", NULL, NULL);
    
    glfwMakeContextCurrent(win);
    glfwSwapInterval(1);
    ImGui::CreateContext();
    ImGuiIO &io = ImGui::GetIO(); (void) io;

    ImGui_ImplGlfw_InitForOpenGL(win, true);
    ImGui_ImplOpenGL3_Init(glsl_version);

output: 'output/Debug/amalgam' terminated by signal SIGSEGV (Address boundary error)
output from lldb:
image

**Version/Branch of Dear ImGui:** imgui/1.83 glfw/3.3.4 glew/2.2.0 all pulled from Conan **Back-end/Renderer/Compiler/OS** Back-ends: imgui_impl_glfw.cpp + imgui_impl_opengl3.cpp Compiler: clang(++) Operating System: archlinux **My Issue/Question:** ImGui_ImplOpenGL3_init(); gives a segmentation fault when called Main.cpp ```cpp const char* glsl_version = "#version 130"; glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); glfwInit(); glewInit(); IMGUI_CHECKVERSION(); GLFWwindow* win = glfwCreateWindow(500, 400, "window", NULL, NULL); glfwMakeContextCurrent(win); glfwSwapInterval(1); ImGui::CreateContext(); ImGuiIO &io = ImGui::GetIO(); (void) io; ImGui_ImplGlfw_InitForOpenGL(win, true); ImGui_ImplOpenGL3_Init(glsl_version); ``` output: `'output/Debug/amalgam' terminated by signal SIGSEGV (Address boundary error)` output from lldb: ![image](https://user-images.githubusercontent.com/35979547/123724506-14a1d480-d852-11eb-8ec7-c6d13c0f04e1.png)
wbehrens-on-gh commented 2021-06-29 01:51:19 +00:00 (Migrated from github.com)

Should have probably included this, when stepping into the function with lldb:
image

Should have probably included this, when stepping into the function with lldb: ![image](https://user-images.githubusercontent.com/35979547/123724798-95f96700-d852-11eb-8f08-3d6d97822ff3.png)
ocornut commented 2021-06-29 11:23:32 +00:00 (Migrated from github.com)

Is the imgui_impl_opengl3 backend compiled to use Glew and not gl3w?

// I just realized that adding the call here defeat the helper debug/test code added between line 230 and 270 to help people detect that the right loader is used. I will move them in the other order.

Is the imgui_impl_opengl3 backend compiled to use Glew and not gl3w? // ~~I just realized that adding the call here defeat the helper debug/test code added between line 230 and 270 to help people detect that the right loader is used. I will move them in the other order.~~
ocornut commented 2021-06-29 12:41:11 +00:00 (Migrated from github.com)

Hello,

Can't strikeout the second sentence from my previous message (messages sent via email don't have markdown marketing) but you can ignore the second sentence.

Can you check the value of local variable const char* gl_loader at the time of the crash?

Linking to #4170

Hello, Can't strikeout the second sentence from my previous message (messages sent via email don't have markdown marketing) but you can ignore the second sentence. Can you check the value of local variable `const char* gl_loader` at the time of the crash? Linking to #4170
wbehrens-on-gh commented 2021-06-30 00:46:18 +00:00 (Migrated from github.com)

@ocornut Printing gl_loader shows that it does detect it as GLEW and not GL3W

@ocornut Printing gl_loader shows that it does detect it as GLEW and not GL3W
ocornut commented 2021-06-30 07:27:13 +00:00 (Migrated from github.com)

You need to use the right define so it that cpp will use glew then.

You need to use the right define so it that cpp will use glew then.
ocornut commented 2021-06-30 14:20:48 +00:00 (Migrated from github.com)

I will amend the commentary around that section with:

const char* extension = (const char*)glGetStringi(GL_EXTENSIONS, i); // IF YOU GET A CRASH HERE: READ ABOVE.

"Above" refer to:

// Make an arbitrary GL call (we don't actually need the result)
// IF YOU GET A CRASH HERE: it probably means that you haven't initialized the OpenGL function loader used by THIS FILE.
// Desktop OpenGL 3/4 need a function loader. See the IMGUI_IMPL_OPENGL_LOADER_xxx explanation above.
// Check the value of the "gl_loader" variable and confirm it is the same as the loader you intended to use.
GLint current_texture;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &current_texture);

You can add #define IMGUI_IMPL_OPENGL_LOADER_GLEW in your imconfig file or make sure that the paths are setup in a way where the compile-time attempt at autodetection in imgui_impl_opengl3.cpp will succeed.

I will amend the commentary around that section with: ```cpp const char* extension = (const char*)glGetStringi(GL_EXTENSIONS, i); // IF YOU GET A CRASH HERE: READ ABOVE. ``` "Above" refer to: ```cpp // Make an arbitrary GL call (we don't actually need the result) // IF YOU GET A CRASH HERE: it probably means that you haven't initialized the OpenGL function loader used by THIS FILE. // Desktop OpenGL 3/4 need a function loader. See the IMGUI_IMPL_OPENGL_LOADER_xxx explanation above. // Check the value of the "gl_loader" variable and confirm it is the same as the loader you intended to use. GLint current_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &current_texture); ``` You can add `#define IMGUI_IMPL_OPENGL_LOADER_GLEW` in your imconfig file or make sure that the paths are setup in a way where the compile-time attempt at autodetection in imgui_impl_opengl3.cpp will succeed.
wbehrens-on-gh commented 2021-06-30 19:15:13 +00:00 (Migrated from github.com)

Adding that define doesn't fix the issue, I'm using GLEW which means that it detects the right openGL loader

Adding that define doesn't fix the issue, I'm using GLEW which means that it detects the right openGL loader
ocornut commented 2021-07-01 18:54:37 +00:00 (Migrated from github.com)

Can you confirm you are getting that crash and the value for gl_loader says “glew” and NOT anything else?

Can you confirm you are getting that crash and the value for gl_loader says “glew” and NOT anything else?
mayuso commented 2021-07-21 18:37:19 +00:00 (Migrated from github.com)

I'm not the author of the issue, but have the same crash in the same point in my code. My debugger prints gl_loader as "GLEW", not "glew", could that be that the problem?. I'll list all the variables that I assume are relevant (I really new to OpenGL and ImGui, so please correct me if I'm wrong):

major = 3
minor = 3
glsl_version = "#version 330"
gl_loader = "GLEW"

The exact versions of glew and glfw that I use are the following:

https://github.com/Perlmint/glew-cmake/tree/883e35a3d493d93fa27da5abb6225654c360f9d0
https://github.com/glfw/glfw/tree/6876cf8d7e0e70dc3e4d7b0224d08312c9f78099

The exact error that I see is the following:

Exception has occurred: W32/0xC0000005
Unhandled exception at 0x0000000000000000 in Gui.exe: 0xC0000005: Access violation executing location 0x0000000000000000.

I also have all the initialization glfw and glew code in a shared library that I then use in my Gui project, and I am starting to believe that is the problem, maybe I am losing the current context or any other relevant init setup because I create it inside a shared library? I know this library works because I have a Sandbox project where I import it the same way and draw a couple triangles. But calling ImGui_ImplOpenGL3_Init("#version 330"); crashes.

Please tell me if I can do anything else.

I'm not the author of the issue, but have the same crash in the same point in my code. My debugger prints gl_loader as "GLEW", not "glew", could that be that the problem?. I'll list all the variables that I assume are relevant (I really new to OpenGL and ImGui, so please correct me if I'm wrong): major = 3 minor = 3 glsl_version = "#version 330" gl_loader = "GLEW" The exact versions of glew and glfw that I use are the following: [https://github.com/Perlmint/glew-cmake/tree/883e35a3d493d93fa27da5abb6225654c360f9d0](https://github.com/Perlmint/glew-cmake/tree/883e35a3d493d93fa27da5abb6225654c360f9d0) [https://github.com/glfw/glfw/tree/6876cf8d7e0e70dc3e4d7b0224d08312c9f78099](https://github.com/glfw/glfw/tree/6876cf8d7e0e70dc3e4d7b0224d08312c9f78099) The exact error that I see is the following: Exception has occurred: W32/0xC0000005 Unhandled exception at 0x0000000000000000 in Gui.exe: 0xC0000005: Access violation executing location 0x0000000000000000. I also have all the initialization glfw and glew code in a shared library that I then use in my `Gui` project, and I am starting to believe that is the problem, maybe I am losing the current context or any other relevant init setup because I create it inside a shared library? I know this library works because I have a `Sandbox` project where I import it the same way and draw a couple triangles. But calling `ImGui_ImplOpenGL3_Init("#version 330");` crashes. Please tell me if I can do anything else.
mayuso commented 2021-07-22 13:45:36 +00:00 (Migrated from github.com)

Ok so after testing a little more, I've found that the exact same code works fine when compiling with GCC on Pop_os and doesn't work on VS2019 on windows, compiles, but crashes when running. I am now sure the problem is not imgui, so I'll leave it here. Thank you :)

Ok so after testing a little more, I've found that the exact same code works fine when compiling with GCC on Pop_os and doesn't work on VS2019 on windows, compiles, but crashes when running. I am now sure the problem is not imgui, so I'll leave it here. Thank you :)
ocornut commented 2021-07-22 20:31:26 +00:00 (Migrated from github.com)

@mayuso Could you step in with a debugger and tell exactly which function call is crashing?
It is glGetStringi() ?
Given the confirmation above, if you can help reproducing this we can work on a fix.

@mayuso Could you step in with a debugger and tell exactly which function call is crashing? It is `glGetStringi()` ? Given the confirmation above, if you can help reproducing this we can work on a fix.
mayuso commented 2021-07-23 06:59:53 +00:00 (Migrated from github.com)

Ok, I've gone step by step and my problem wasn't actually glGetStringi(), it was GL_EXTENSIONS, it was null. I wasn't linking glew correctly on windows (I think?). CMake is hard I guess. The strange thing is that I've changed the windows code to build glew as shared library and just works, but that doesn't work on Pop_os. So right now I build differently for each OS, I hate it. I need to learn more so this kind of problems don't make everyone lose time.

Ok, I've gone step by step and my problem wasn't actually `glGetStringi()`, it was `GL_EXTENSIONS`, it was null. I wasn't linking glew correctly on windows (I think?). CMake is hard I guess. The strange thing is that I've changed the windows code to build glew as shared library and just works, but that doesn't work on Pop_os. So right now I build differently for each OS, I hate it. I need to learn more so this kind of problems don't make everyone lose time.
ocornut commented 2021-08-19 14:26:49 +00:00 (Migrated from github.com)

We now should have shot down this problem with #4445

It is possible that the extension polling mechanism may fail on some setup, in which case we can open that. But please first confirm the issue with latest code as posted in #4445

We now should have shot down this problem with #4445 It is _possible_ that the extension polling mechanism may fail on some setup, in which case we can open that. But please first confirm the issue with latest code as posted in #4445
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#4279
No description provided.