当我运行以下代码(或我尝试过的任何变体)时,出现总线错误。它汇编得很好。
#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;
}
}
我在Mac OSX上运行它。如果glGetShaderInfoLog被注释掉它(glGetProgramInfoLog导致相同的错误),它工作正常。以下是崩溃报告中的一些重要信息。
异常类型:EXC_BAD_ACCESS(SIGBUS)
例外代码:KERN_PROTECTION_FAILURE位于0x000000010cd5c0f0
异常注意:EXC_CORPSE_NOTIFY
终止信号:总线错误:10
终止原因:命名空间SIGNAL,代码0xa
终止进程:exc handler [0]
VM区域接近0x10cd5c0f0:
- &GT; __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程序0x000000010cd357af Shader :: validate()+ 127
4程序0x000000010cd35575 Shader :: compile()+ 1621
5程序0x000000010cd349ed Program :: link()+ 45
6程序0x000000010cd3235e Object :: Object(char *,char *,char *,char *)+ 222
7程序0x000000010cd32c65 Object :: Object(char *,char *,char *,char *)+ 53
8程序0x000000010cd2e7c0 main + 4576
9 libdyld.dylib 0x00007fffcc2ff255 start + 1
答案 0 :(得分:2)
glGetShaderInfoLog
的最后一个参数告诉它将日志写入的位置。你传给了一个字符串文字"\0"
。您告诉它用日志文件覆盖此字符串文字。
覆盖字符串文字是未定义的行为;在实践中,它通常会导致程序崩溃。即使你可以覆盖它,你也无法提供足够的空间。
您应该改为分配一些空间来保存日志。像这样:
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()