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

ImGui_ImplOpenGL3_init() returns a segfault with GLEW #4279

Closed
wbehrens-on-gh opened this issue Jun 29, 2021 · 14 comments
Closed

ImGui_ImplOpenGL3_init() returns a segfault with GLEW #4279

wbehrens-on-gh opened this issue Jun 29, 2021 · 14 comments

Comments

@wbehrens-on-gh
Copy link

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

@wbehrens-on-gh
Copy link
Author

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

@ocornut
Copy link
Owner

ocornut commented Jun 29, 2021 via email

@ocornut
Copy link
Owner

ocornut commented Jun 29, 2021

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
Copy link
Author

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

@ocornut
Copy link
Owner

ocornut commented Jun 30, 2021 via email

@ocornut
Copy link
Owner

ocornut commented Jun 30, 2021

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.

@ocornut ocornut closed this as completed Jun 30, 2021
@wbehrens-on-gh
Copy link
Author

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

@ocornut
Copy link
Owner

ocornut commented Jul 1, 2021

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

@mayuso
Copy link

mayuso commented Jul 21, 2021

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.

@mayuso
Copy link

mayuso commented Jul 22, 2021

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
Copy link
Owner

ocornut commented Jul 22, 2021

@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.

@ocornut ocornut reopened this Jul 22, 2021
@mayuso
Copy link

mayuso commented Jul 23, 2021

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
Copy link
Owner

ocornut commented Aug 19, 2021

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 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
@ocornut @mayuso @wbehrens-on-gh and others