glewInit() gives an Segmentation fault

The code runs well in my Macbook, but it gives me error in a linux machine (It compiles well). I traced down and it is

GLenum err = glewInit();

that gives me the error, and the error message says simply

Segmentation fault (core dumped)

I looked up online, and tried to put

glewExperimental = GL_TRUE;

before calling

GLenum err = glewInit();

, still the same error.

How do I fix it? I need help.

Here on my Linux box, glewInit() can be the very first call in main().

Let’s see some code.

Also bring this up in your favorite debugger and see if you can get more useful data (as far as a stack trace or more details on why you’re getting a segfault). E.g. bring up in gdb, run, then do a “where”.

Finally, let’s see how you’ve got it linked. Do an “ldd <exec>”.

[QUOTE=Dark Photon;1286799]Here on my Linux box, glewInit() can be the very first call in main().

Let’s see some code.

Also bring this up in your favorite debugger and see if you can get more useful data (as far as a stack trace or more details on why you’re getting a segfault). E.g. bring up in gdb, run, then do a “where”.

Finally, let’s see how you’ve got it linked. Do an “ldd <exec>”.[/QUOTE]

Here is a testing code which works on my Mac OSX, but not on this Ubuntu 16.04.

// Include standard headers
#include <stdio.h>
#include <stdlib.h>
// Include GLEW. Always include it before gl.h and glfw.h, since it's a bit magic.
#include <GL/glew.h>
// Include GLFW
#include <GLFW/glfw3.h>
// Include GLM
#include <glm/glm.hpp>
using namespace glm;

int main(){

// Initialise GLFW
if( !glfwInit() )
{
    fprintf( stderr, "Failed to initialize GLFW
" );
    return -1;
}

glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL 

// Open a window and create its OpenGL context
GLFWwindow* window; // (In the accompanying source code, this variable is global)
window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL);
if( window == NULL ){
    fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.
" );
    glfwTerminate();
    return -1;
}
glfwMakeContextCurrent(window); // Initialize GLEW
glewExperimental=true; // Needed in core profile
if (glewInit() != GLEW_OK) {
    fprintf(stderr, "Failed to initialize GLEW
");
    return -1;
}
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

do{
    // Draw nothing, see you in tutorial 2 !

    // Swap buffers
    glfwSwapBuffers(window);
    glfwPollEvents();

} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );

}

The code compiles well in Ubuntu 16.04, but it gives an segmentation fault error while running. Here is the error message
[NOTE]Segmentation fault (core dumped)[/NOTE]

I’m not very familiar with debugging tools, usually I just output to debug. But running gdb a.out followed by where does give some output saying

(gdb) r
Starting program: /home/paul/Research/Samples/GL_examples/a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library “/lib/x86_64-linux-gnu/libthread_db.so.1”.

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) where
#0 0x0000000000000000 in ?? ()
#1 0x00007ffff7ba19c7 in glxewInit () from /usr/lib64/libGLEW.so.2.0
#2 0x00007ffff7ba9cbd in glewInit () from /usr/lib64/libGLEW.so.2.0
#3 0x0000000000403c2e in main ()
(gdb)

Here is an output of ldd a.out

linux-vdso.so.1 => (0x00007ffe560d5000)
libGLEW.so.2.0 => /usr/lib64/libGLEW.so.2.0 (0x00007f6da0fd9000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f6da0cb0000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f6da0a92000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f6da088e000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f6da0686000)
libXrandr.so.2 => /usr/lib/x86_64-linux-gnu/libXrandr.so.2 (0x00007f6da047a000)
libXinerama.so.1 => /usr/lib/x86_64-linux-gnu/libXinerama.so.1 (0x00007f6da0277000)
libXxf86vm.so.1 => /usr/lib/x86_64-linux-gnu/libXxf86vm.so.1 (0x00007f6da0071000)
libXcursor.so.1 => /usr/lib/x86_64-linux-gnu/libXcursor.so.1 (0x00007f6d9fe66000)
libX11.so.6 => /usr/lib/x86_64-linux-gnu/libX11.so.6 (0x00007f6d9fb2c000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f6d9f763000)
libGL.so.1 => /usr/lib/nvidia-381/libGL.so.1 (0x00007f6d9f4d2000)
/lib64/ld-linux-x86-64.so.2 (0x0000561bea047000)
libXext.so.6 => /usr/lib/x86_64-linux-gnu/libXext.so.6 (0x00007f6d9f2c0000)
libXrender.so.1 => /usr/lib/x86_64-linux-gnu/libXrender.so.1 (0x00007f6d9f0b6000)
libXfixes.so.3 => /usr/lib/x86_64-linux-gnu/libXfixes.so.3 (0x00007f6d9eeaf000)
libxcb.so.1 => /usr/lib/x86_64-linux-gnu/libxcb.so.1 (0x00007f6d9ec8d000)
libGLX.so.0 => /usr/lib/nvidia-381/libGLX.so.0 (0x00007f6d9ea5c000)
libGLdispatch.so.0 => /usr/lib/nvidia-381/libGLdispatch.so.0 (0x00007f6d9e773000)
libXau.so.6 => /usr/lib/x86_64-linux-gnu/libXau.so.6 (0x00007f6d9e56f000)
libXdmcp.so.6 => /usr/lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007f6d9e368000)

Seems to work fine here on my OpenSuSE Linux box (glfw 3.2, glew 1.9.0, NVidia GL drivers ver 378.13).

Why don’t I see glfw in your shared library dependency list?

Also I notice you’re linking with glew 2.0, whereas I’m linking with glew 1.9. Not sure if that’s related to your problem or not.

What GLFW are you linking with and what driver are you trying this on? For the latter, let’s see the output of this:

glxinfo | egrep ‘OpenGL (vendor|renderer|version|shading|core)’

Also, give how you compile (makefile, gcc command line…).
Also compile again with ‘-g -ggdb’ and run your program again within gdb and give the backtrace again.

Yes, it is very strange why it doesn’t show glfw in library link list. I use the following command for compiling:
export GL_LIBRARIES="-lGL -lGLU -lglut -lGLEW -lglfw3 $(pkg-config --static --libs x11 xrandr xi xxf86vm glew glfw3)"
g++ simpleTest.cpp $GL_LIBRARIES

And if I echo $GL_LIBRARIES, it gives me
[NOTE]-lGL -lGLU -lglut -lGLEW -lglfw3 -L/usr/local/lib -L/usr/lib64 -lXi -lGLEW -lGLU -lm -lGL -lm -lpthread -ldl -ldrm -lXdamage -lX11-xcb -lxcb-glx -lxcb-dri2 -lglfw3 -lrt -lm -ldl -lXrandr -lXinerama -lXxf86vm -lXext -lXcursor -lXrender -lXfixes -lX11 -lpthread -lxcb -lXau -lXdmcp[/NOTE]

The command
find . -name libglfw3* 2> /dev/null
results in the following output:
[NOTE]./usr/local/lib/libglfw3.a
./home/paul/Downloads/glfw-3.2.1/src/libglfw3.a[/NOTE]

The command
glxinfo | egrep ‘OpenGL (vendor|renderer|version|shading|core)’
gives the following output:
[NOTE]OpenGL vendor string: NVIDIA Corporation
OpenGL renderer string: GeForce GTX 1050 Ti/PCIe/SSE2
OpenGL core profile version string: 4.5.0 NVIDIA 381.09
OpenGL core profile shading language version string: 4.50 NVIDIA
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile
OpenGL core profile extensions:
OpenGL version string: 4.5.0 NVIDIA 381.09
OpenGL shading language version string: 4.50 NVIDIA[/NOTE]

Compiling the code with g++ simpleTest.cpp $GL_LIBRARIES -g -ggdb and then run gdb a.out gives the following output
NOTE r
Starting program: /home/paul/Research/Samples/GL_examples/a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library “/lib/x86_64-linux-gnu/libthread_db.so.1”.

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) where
#0 0x0000000000000000 in ?? ()
#1 0x00007ffff7ba19c7 in glxewInit () from /usr/lib64/libGLEW.so.2.0
#2 0x00007ffff7ba9cbd in glewInit () from /usr/lib64/libGLEW.so.2.0
#3 0x0000000000403c2e in main () at simpleTest.cpp:37
[/NOTE]

I’m wondering why there is no libglfw3.so file in my build. Do you have libglfw3.so in your build?

Moreover, after taking out the part of the code that’s after the line of glewinit, I have the following code, which runs fine. So that’s an indication of glfw3 is still linked?

// Include standard headers
#include <stdio.h>
#include <stdlib.h>
// Include GLEW. Always include it before gl.h and glfw.h, since it's a bit magic.
#include <GL/glew.h>
// Include GLFW
#include <GLFW/glfw3.h>
// Include GLM
#include <glm/glm.hpp>
using namespace glm;

int main(){

// Initialise GLFW
if( !glfwInit() )
{
    fprintf( stderr, "Failed to initialize GLFW
" );
    return -1;
}

glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL 

// Open a window and create its OpenGL context
GLFWwindow* window; // (In the accompanying source code, this variable is global)
window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL);
if( window == NULL ){
    fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.
" );
    glfwTerminate();
    return -1;
}
glfwMakeContextCurrent(window); // Initialize GLEW

}

Yes. That is what gets installed with the glfw packages on OpenSUSE:


> rpm -ql libglfw3
/usr/lib64/libglfw.so.3
/usr/lib64/libglfw.so.3.2
/usr/share/doc/packages/libglfw3
/usr/share/doc/packages/libglfw3/COPYING.txt
/usr/share/doc/packages/libglfw3/README.md

> rpm -ql libglfw-devel
/usr/include/GLFW
/usr/include/GLFW/glfw3.h
/usr/include/GLFW/glfw3native.h
/usr/lib64/cmake/glfw3
/usr/lib64/cmake/glfw3/glfw3Config.cmake
/usr/lib64/cmake/glfw3/glfw3ConfigVersion.cmake
/usr/lib64/cmake/glfw3/glfw3Targets-none.cmake
/usr/lib64/cmake/glfw3/glfw3Targets.cmake
/usr/lib64/libglfw.so
/usr/lib64/pkgconfig/glfw3.pc
/usr/share/doc/packages/libglfw-devel
/usr/share/doc/packages/libglfw-devel/boing.c
/usr/share/doc/packages/libglfw-devel/gears.c
/usr/share/doc/packages/libglfw-devel/heightmap.c
/usr/share/doc/packages/libglfw-devel/particles.c
/usr/share/doc/packages/libglfw-devel/simple.c
/usr/share/doc/packages/libglfw-devel/splitview.c
/usr/share/doc/packages/libglfw-devel/wave.c

It looks like you’ve got a locally-built static (.a) GLFW library there. You might try installing the Ubuntu GLFW packages and recompile/relink with those, in case your problem is with that locally-built library. It could also be that the GLFW headers you’re compiling with don’t match your library.

Moreover, the following code works fine. So that’s an indication of glfw3 is still linked?

I thought you said: “The code compiles well in Ubuntu 16.04, but it gives an segmentation fault error while running.”.

But as to your code linking, yes it appears you’re linked with a static GLFW library (libglfw.a) rather than a dynamic GLFW library (libglfw.so).

It was the library issue. I now compiled all the libraries again, including GLEW, glfw, glm, and glut, using apt-get, instead of doing configure, make install and cmake kind of stuff. And it saves a lot of trouble, now I get the dynamic library for everything. And the code runs well.

Thank you!