#OpenGL exiting with 139

6 messages · Page 1 of 1 (latest)

placid mantle
#

I'm following alone with The Cherno's youtube tutorial on OpenGL with glfw in C++, and I believe that I have written the same code that he has in one of his videos, but I am running into a problem. The code that I have provided runs, the window opens for a split second before I get

Process finished with exit code 139 (interrupted by signal 11:SIGSEGV)

Not entirely sure how to debug this either, but I had this same problem before I wrote all the shader compilation code, and just had the vertex buffer setup. I figured maybe because I didn't have a shader (this assumes my GPU didn't already imlpement one,) that maybe that was the problem. So assuming I implemented the shader correctly, that didn't fix it

    unsigned int vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6, vertices, GL_STATIC_DRAW);

    while (!glfwWindowShouldClose(window)) {
        // code

        glDrawArrays(GL_TRIANGLES, 0, 3);

        // code
    }

So I'm assuming the problem persists with that.

See full code under:

foggy plazaBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

placid mantle
#
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

/**
 * @param type specifies the type of shader, not an opengl datatype to provide compatibility with other libs
 */
static unsigned int compileShader(unsigned int type, const std::string& src) {
    unsigned int id = glCreateShader(type);
    const char* rawSrc = src.c_str();
    glShaderSource(id, 1, &rawSrc, nullptr);
    glCompileShader(id);

    // error handling
    int result;
    // put status of compilation into result
    glGetShaderiv(id, GL_COMPILE_STATUS, &result);
    if (result == GL_FALSE) {
        // get and print error message
        int length;
        glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
        char* message = new char[length];
        glGetShaderInfoLog(id, length, &length, message);
        std::cout << "Failed to compile the " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << " shader." << std::endl;
        std::cout << message << std::endl;
        delete[] message;
        // delete failed shader
        glDeleteShader(id);
        return 0;
    }


    return id;
}

static unsigned int createShaders(const std::string& vertexSrc, const std::string& pixelSrc) {
    unsigned int program = glCreateProgram();
    // fixme: this assumes compilation worked
    unsigned int vertex  = compileShader(GL_VERTEX_SHADER, vertexSrc);
    unsigned int pixel   = compileShader(GL_FRAGMENT_SHADER, pixelSrc);

    glAttachShader(program, vertex);
    glAttachShader(program, pixel);
    glLinkProgram(program);
    glValidateProgram(program);

    // cleanup
    glDeleteShader(vertex);
    glDeleteShader(pixel);

    return program;
}
#
int main() {
    if (!glfwInit()) {
      return -1;
    }
    if (!glewInit()) {
      return -1;
    }

    /* Create a windowed mode window and its OpenGL context */
    GLFWwindow *window = glfwCreateWindow(1280, 720, "OpenGL", nullptr, nullptr);

    if (!window) {
        glfwTerminate();
        throw std::runtime_error("Unable to create glfw window");
    }

    GLFWmonitor *monitor = glfwGetPrimaryMonitor();
    const GLFWvidmode *mode = glfwGetVideoMode(monitor);

    // center the window
    glfwSetWindowPos(window, (mode->width - 1280) / 2, (mode->height - 720) / 2);

    /* Make the window's context current */
    glfwMakeContextCurrent(window);
    // Set background clear color
    glClearColor(0.3f, 0.4f, 0.5f, 1.0f);

    float vertices[6] = {
            0.0f, 0.5f,
            -0.5f, 0.0f,
            0.5f, 0.0f
    };

    unsigned int vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6, vertices, GL_STATIC_DRAW);

    // define format of vertices
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, nullptr);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);

    std::string vertexSrc =
            "#version 330 core"

            "layout(location = 0) in vec4 position;"

            "void main() {"
            "    gl_Position = position;"
            "}";
    std::string pixelSrc  =
            "#version 330 core"

            "layout(location = 0) out vec4 color;"

            "void main() {"
            "    color = vec4(1.0, 0.0, 0.0, 1.0);"
            "}";

    glUseProgram(createShaders(vertexSrc, pixelSrc));

    while (!glfwWindowShouldClose(window)) {
        glClear(GL_COLOR_BUFFER_BIT);

        glDrawArrays(GL_TRIANGLES, 0, 3);

        glfwSwapBuffers(window);

        glfwPollEvents();
    }

    glfwTerminate();

    return 0;
}
#

This is all in the same file

#

and it's also all of my code. I acknowledge the poor structure, I'm not trying to make it great, I'm just trying to get stuff t ohappen.