OpenGL getShaderInfoLog 总线错误 10

OpenGL getShaderInfoLog Bus Error 10

当我 运行 以下代码(或我尝试过的任何变体)时,出现总线错误。它编译得很好。

#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*) "[=10=]";
        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;
    }
}

我 运行 在 Mac OSX 上收看它。如果 glGetShaderInfoLog 被注释掉,它工作正常(虽然 glGetProgramInfoLog 导致相同的错误)。这是崩溃报告中的一些重要信息。

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

glGetShaderInfoLog 的最后一个参数告诉它把日志写到哪里。您向它传递了一个字符串文字 "[=12=]"。你告诉它用日志文件覆盖这个字符串文字。

覆盖字符串文字是未定义的行为;实际上,它通常会导致您的程序崩溃。即使您可以覆盖它,您仍然没有提供足够的 space。

您应该分配一些 space 来保存日志。像这样:

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()