2

Consider a very simple OpenGL program:

#include <GL/glut.h>

static void RenderScene()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowSize(500, 500);
    glutCreateWindow("OpenGL Test");
    glutDisplayFunc(RenderScene);
    glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
    glutMainLoop();

    return 0;
}

This compiles and runs fine, displaying a grey window as expected.

Then, I introduce three variables into the main function, before the OpenGL processing. The code becomes:

#include <string>
#include <GL/glut.h>


static void RenderScene()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glutSwapBuffers();
}


int main(int argc, char** argv)
{
    int x = 5;
    char y = 'a';
    std::string z = "abc";

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowSize(500, 500);
    glutCreateWindow("OpenGL Test");
    glutDisplayFunc(RenderScene);
    glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
    glutMainLoop();

    return 0;
}

Compiling this is fine, but if I run it, I receive a segmentation fault error. Now, if I comment out the line std::string z = "abc";, then it runs fine without any errors.

So, for some reason, declaring a string variable is causing a segmentation fault here, but not with any other variable types.

If I remove all the OpenGL code, then it runs fine. But if I remove all the OpenGL code except for just one line, such as glutInit(&argc, argv);, then it still causes the segmentation fault.

Any ideas on what is going on?

Karnivaurus
  • 22,823
  • 57
  • 147
  • 247
  • I had a similar problem once. I found adding glutGet(GLUT_DISPLAY_MODE_POSSIBLE) (and exiting if it returned false) helped. It felt like the glutGet() was performing some essential initialization. But I never was able to actually trace it down and prove it. – Dave S Jun 20 '15 at 04:21
  • are you calling with commandline arguments? – M.M Jun 20 '15 at 05:16
  • include compiler name, version & platform – M.M Jun 20 '15 at 05:16
  • Also looking at your other probably related question here: http://stackoverflow.com/questions/30949880/runtime-error-with-glsl-shaders-inconsistency-detected-by-ld-so, it almost looks like you have a problem with the standard C++ library on your system. Maybe the headers you include and the library that gets linked/loaded do not match, or something in that style. – Reto Koradi Jun 20 '15 at 07:02
  • seems to be a duplicate: http://stackoverflow.com/questions/31579243/segmentation-fault-before-main-when-using-glut-and-stdstring – Algoman Nov 02 '16 at 23:49

1 Answers1

0

I'm having similar problem too, but mine was on Linux. Is your on Linux or Windows? Do you have any graphic card driver install?

For my case, I'm running on Ubuntu with nVidia installed, I notice that when I link my program with libGL.so located at /usr/lib/x86_64-linux-gnu, it will cause segmentation fault. Then I also found out there is another libGL.so located at /usr/lib/nvidia-352, this will link without crash.

Hope this help.

huahsin68
  • 6,819
  • 20
  • 79
  • 113