3

I have been reading a tutorial on modern OpenGL and GLSL, but when I run the examples for the project, it sometimes breaks. The code throws:

terminate called after throwing an instance of '__gnu_cxx::recursive_init_error'
  what():  std::exception
Aborted (core dumped)

I then decided to remake it to see if it was a problem with the dependencies in it's source, but the same error occurs. If I run it enough times, it will eventually produce the desired output.

I have looked for other answers but they are either:

  1. About threads (which I don't believe I have in my code);
  2. In a foreign language (which comes to the same conclusion, after being translated); or
  3. Point to an obscure location in the std implementation talking about static objects (therefore no help at all).

main.cpp:

#include <GL/glew.h>
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>

GLfloat vertices[] = 
{
    0.5, 0.5, 0.0, 1.0,
    0.5, -0.5, 0.0, 1.0,
    -0.5, -0.5, 0.0, 1.0,
    -0.5, 0.5, 0.0, 1.0,
};

GLuint program;
GLuint vbo;
GLuint vao;

GLuint loadShader(GLenum shaderType, const char *filename)
{
    GLuint shader = glCreateShader(shaderType);
    std::ifstream file(filename);
    std::stringstream data;
    data << file.rdbuf();
    file.close();

    const char *shaderData = data.str().c_str();
    glShaderSource(shader, 1, &shaderData, NULL);

    glCompileShader(shader);

    return shader;
}

GLuint createProgram(std::vector<GLuint> *list)
{
    GLuint prog = glCreateProgram();

    for(int i = 0; i < list->size(); i++)
    {
        glAttachShader(prog, list->at(i));
    }

    glLinkProgram(prog);

    for(int i = 0; i < list->size(); i++)
    {
        glDetachShader(prog, list->at(i));
    }

    return prog;
}

void display(void)
{
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glUseProgram(program);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);

    glDrawArrays(GL_QUADS, 0, 4);
    
    glDisableVertexAttribArray(0);
    glUseProgram(0);

    glutSwapBuffers();
}

void init(void)
{
    std::vector<GLuint> shaderList;

    shaderList.push_back(loadShader(GL_VERTEX_SHADER, "vertexshader.vert"));
    shaderList.push_back(loadShader(GL_FRAGMENT_SHADER, "fragmentshader.frag"));

    program = createProgram(&shaderList);

    std::for_each(shaderList.begin(), shaderList.end(), glDeleteShader);

    glGenBuffers(1, &vbo);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glGenBuffers(1, &vao);
    glBindVertexArray(vao);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE);
    glutInitWindowSize(250, 250);
    glutCreateWindow("OpenGL");

    glewInit();

    init();
    glutDisplayFunc(display);
    glutMainLoop();

    return 0;
}

vertexshader.vert:

#version 330

layout(location = 0) in vec4 pos;

void main()
{
    gl_Position = pos;
}

fragmentshader.frag:

#version 330

out vec4 frag;

void main()
{
    frag = vec4(0.0f, 0.0f, 1.0f, 1.0f);
}

Tutorial Download

genpfault
  • 51,148
  • 11
  • 85
  • 139
Evan Hess
  • 51
  • 3
  • 2
    Double-check how long the temporary returned by `data.str()` is going to last. Hint: [not very long](https://stackoverflow.com/questions/21534682/stringstreamstr-copy-lifetime) :) – genpfault Jul 15 '20 at 03:48
  • @genpfault Wow, I read the page and added a ```std::string``` to hold ```data.str()``` and it fixed the problem for me. Thank you so much! It actually fixed another problem where the window would capture the top left corner of the screen inside it. I sort of understand why that decision was made, but it felt a little less convenient. With a bit of reworking, it could be a bit more effective. – Evan Hess Jul 15 '20 at 05:07

1 Answers1

2

@genpfault pointed out to me the answer in this statement

Double-check how long the temporary returned by data.str() is going to last. Hint: not very long :)

The resulting change I made (in the "loadShader" function) was:

GLuint loadShader(GLenum shaderType, const char *filename)
{
    ...

    std::string dataStr = data.str();
    const char *shaderData = dataStr.str();
    glShaderSource(shader, 1, &shaderData, NULL);

    ...
}

This actually fixed both the original issue and another visual issue in which the window rendered the top left corner of my screen inside it.

Thanks again!

Evan Hess
  • 51
  • 3