c++macosopenglbus-errorsigbus

OpenGL getShaderInfoLog Bus Error 10


When I run the following code (or any of the variations I've tried), I get a bus error. It compiles fine.

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <fstream>
#include <iostream>
#include <stream>
void main() {
    /* * * * * * * * * * * * * * * * * *
     * Code that loads the shader file.*
     * This works fine, error is later.*
     * * * * * * * * * * * * * * * * * */
    /* * * * * * * * * * * * * * * * * *
     * Code that generates OpenGL      *
     * shader and compiles it. Works.  *
     * * * * * * * * * * * * * * * * * */

    // Check for errors compiling shader
    bool status;
    glGetShaderiv(shaderId, GL_COMPILE_STATUS, &status);
    if(status == GL_FALSE) {
        // Compiler errors
        GLchar* log = (GLchar*) "\0";
        GLint len = 0;
        GLint rLen = 0;
        glGetShaderiv(this->shaderId, GL_INFO_LOG_LENGTH, &len);
        // Next line causes bus error 10
        glGetShaderInfoLog(this->shaderId, len, &rLen, log);
        std::cerr << "Error compiling shader: " << this->filename << ":\n\t" << log << std::endl;
    }
}

I am running it on Mac OSX. It works fine if the glGetShaderInfoLog is commented out (glGetProgramInfoLog causes the same error though). Here is some important looking info from the crash report.

Exception Type: EXC_BAD_ACCESS (SIGBUS)

Exception Codes: KERN_PROTECTION_FAILURE at 0x000000010cd5c0f0

Exception Note: EXC_CORPSE_NOTIFY

Termination Signal: Bus error: 10

Termination Reason: Namespace SIGNAL, Code 0xa

Terminating Process: exc handler [0]

VM Regions Near 0x10cd5c0f0:

--> __TEXT 000000010ccc4000-000000010cd5f000 [ 620K] r-x/rwx SM=COW /Users/USER/*

__DATA 000000010cd5f000-000000010cd64000 [ 20K] rw-/rwx SM=COW /Users/USER/*

0 libsystem_platform.dylib 0x00007fffcc50fefc _platform_memmove$VARIANT$Haswell + 92

1 GLEngine 0x00007fffbbeda274 gleGetString + 52

2 GLEngine 0x00007fffbbe22d20 glGetShaderInfoLog_Exec + 141

3 Program 0x000000010cd357af Shader::validate() + 127

4 Program 0x000000010cd35575 Shader::compile() + 1621

5 Program 0x000000010cd349ed Program::link() + 45

6 Program 0x000000010cd3235e Object::Object(char*, char*, char*, char*) + 222

7 Program 0x000000010cd32c65 Object::Object(char*, char*, char*, char*) + 53

8 Program 0x000000010cd2e7c0 main + 4576

9 libdyld.dylib 0x00007fffcc2ff255 start + 1


Solution

  • The last parameter to glGetShaderInfoLog tells it where to write the log to. You passed it a string literal "\0". You are telling it to overwrite this string literal with the log file.

    Overwriting a string literal is undefined behaviour; in practice it usually causes your program to crash. Even if you could overwrite it, you're not providing enough space anyway.

    You should instead allocate some space to hold the log. Something like this:

    glGetShaderiv(this->shaderId, GL_INFO_LOG_LENGTH, &len);
    vector<char> log(len);
    glGetShaderInfoLog(this->shaderId, log.size(), &rLen, log.data());
    // to get the log as a C string use log.data()