我当时正在学习SDL OpenGL for C++ (我的错误),我不得不将它移植到C.Because C++,这让我有点困惑(顺便说一句)。是的,我可以在网上搜索一个功能替代)。因此,运行这个程序给了我一个错误,似乎是在NVIDIA驱动程序中(顺便说一下)。这张卡是GeForce 105米).Is,这是我的错,还是驱动程序中的错误(我认为是我的错,因为它上的每一个游戏似乎都很好)?
以下是gdb的回溯:
Program received signal SIGSEGV, Segmentation fault.
strlen () at ../sysdeps/x86_64/strlen.S:106
106 ../sysdeps/x86_64/strlen.S: No such file or directory.
(gdb) bt
#0 strlen () at ../sysdeps/x86_64/strlen.S:106
#1 0x00007ffff59cf699 in ?? ()
from /usr/lib/nvidia-340-updates/libnvidia-glcore.so.340.76
#2 0x00007ffff59d1d89 in ?? ()
from /usr/lib/nvidia-340-updates/libnvidia-glcore.so.340.76
#3 0x0000000000401f86 in compileShader ()
#4 0x0000000000401ca6 in compileShaders ()
#5 0x00000000004018b9 in initShaders ()
#6 0x0000000000401a02 in Initilize ()
#7 0x00000000004015ae in main ()下面是compileShader函数(我不会完成整个代码,因为它太长了;),如果您愿意,我仍然可以发布它):
void compileShader(char* filePath, GLuint id) {
//Open the file
FILE *shaderFile = fopen(filePath, "rw");
if (shaderFile == NULL) {
char *str;
sprintf(str,"Failed to open %s", &filePath);
fatalError(str);
}
//File contents stores all the text in the file
char * fileContents = "";
char symbol;
//Get all the lines in the file and add it to the contents
while ((symbol = fgetc(shaderFile)) != EOF ) {
fileContents += symbol;
}
fileContents += EOF;
fclose(shaderFile);
glShaderSource(id, 1, &fileContents, NULL);
glCompileShader(id);
GLint success = 0;
glGetShaderiv(id, GL_COMPILE_STATUS, &success);
if (success == GL_FALSE)
{
glDeleteShader(id);
char *str;
sprintf(str,"Shader %s failed to compile", filePath);
fatalError(str); //Don't worry this just prints out the error
}
}发布于 2015-07-04 19:18:11
这是代码中的一个bug。
给驱动程序的fileContents指针是完全无效的,因此当取消引用该指针时,驱动程序会崩溃。
在C中没有本机字符串数据类型,您只需处理char数组。C不会为你做任何内存管理。因此,char指针上的+=操作符不进行字符串连接。这只是指针运算。您只是在内存中有一个表情字符串,并且fileContent最初指向它。在线旁
fileContents += symbol;通过symbol的数值来增加指针,从而指向空字符串之外的一些内存。
我不想听起来很粗鲁,所以请不要误会我。但我确实建议您先学习想要使用的编程语言,然后再使用OpenGL。
https://stackoverflow.com/questions/31224263
复制相似问题