I have a simple program where I want to check the formatting of my GLFW window but glfwGetVideoMode
causes a segfault.
Here is my code:
if (!glfwInit()) {
VI_ERROR("Couldn't init GLFW\n");
exit(0);
}
glfwWindowHint(GLFW_SAMPLES, 6);
window = glfwCreateWindow(gl_width, gl_height, "GLFW Context", NULL, NULL);
if (!window) {
VI_ERROR("Couldn't open window\n");
exit(0);
}
glfwMakeContextCurrent(window);
gladLoadGL();
GLFWmonitor* wmonitor = glfwGetWindowMonitor(window);
glfwGetVideoMode(wmonitor);
Valgrind says this:
==56501== Invalid read of size 8
==56501== at 0xDF31CB: _glfwPlatformGetVideoMode (in /home/turgut/Desktop/CppProjects/videoo-render/bin/Renderer)
==56501== by 0xDEDE51: glfwGetVideoMode (in /home/turgut/Desktop/CppProjects/videoo-render/bin/Renderer)
==56501== by 0x21F8F6: OpenGL::OpenGLRenderer::OpenGLRenderer(int, int, int, int) (OpenGLRenderer.cpp:27)
==56501== by 0x21BC7A: Application::Run() (Application.cpp:77)
==56501== by 0x21AB6E: main (main.cpp:18)
==56501== Address 0x108 is not stack'd, malloc'd or (recently) free'd
==56501==
==56501==
==56501== Process terminating with default action of signal 11 (SIGSEGV)
==56501== Access not within mapped region at address 0x108
==56501== at 0xDF31CB: _glfwPlatformGetVideoMode (in /home/turgut/Desktop/CppProjects/videoo-render/bin/Renderer)
==56501== by 0xDEDE51: glfwGetVideoMode (in /home/turgut/Desktop/CppProjects/videoo-render/bin/Renderer)
==56501== by 0x21F8F6: OpenGL::OpenGLRenderer::OpenGLRenderer(int, int, int, int) (OpenGLRenderer.cpp:27)
==56501== by 0x21BC7A: Application::Run() (Application.cpp:77)
==56501== by 0x21AB6E: main (main.cpp:18)
==56501== If you believe this happened as a result of a stack
==56501== overflow in your program's main thread (unlikely but
==56501== possible), you can try to increase the size of the
==56501== main thread stack using the --main-stacksize= flag.
==56501== The main thread stack size used in this run was 8388608.
What could I be doing wrong? It's as simple as it gets.
CodePudding user response:
The main problem is that glfwGetWindowMonitor
returns null and thus glfwGetVideoMode
results in a read from a nullptr.
In GLFW, only fullscreen windows are associated with a monitor. Since you pass NULL
as forth parameter to glCreateWindow
and never call glfwSetWindowMonitor
, the current window is in windowed mode and will not have an associated monitor.