在 nvidia-340-updates 中获取分段错误
Getting Segmentation Fault in nvidia-340-updates
我有点在为 C++ 学习 SDL OpenGL(我的错误),我不得不将它移植到 C.Because C++ 对我来说有点混乱(顺便说一句。是的,我可以在网上搜索一个替代函数).所以 运行 这给了我一个错误,似乎是在 NVIDIA 驱动程序中(顺便说一句。显卡是 GeForce 105m)。这是我的错还是驱动程序中的错误(我认为是我,因为它上面的每个游戏似乎工作正常)?
这是 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 函数(我不会完成整个代码,因为它太长了;),如果你愿意,我仍然可以 post 它:
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
}
}
这是您的代码中的错误。
您提供给驱动程序的 fileContents
指针完全无效,因此在取消引用此指针时驱动程序崩溃。
您在 C 中没有本机字符串数据类型,您只使用 char
的数组。 C 不会为你做任何类型的内存管理。因此,char 指针上的 += 运算符不执行字符串连接。这只是指针算术。你只是在内存中有一个情感字符串并且 fileContent
最初指向它。通过行
fileContents += symbol;
你将该指针增加 symbol
的数值,因此指向该空字符串之外的一些内存。
我不想听起来很粗鲁,所以请不要误会我的意思。但我真的建议你先学习你想使用的编程语言,然后再继续使用 OpenGL。
我有点在为 C++ 学习 SDL OpenGL(我的错误),我不得不将它移植到 C.Because C++ 对我来说有点混乱(顺便说一句。是的,我可以在网上搜索一个替代函数).所以 运行 这给了我一个错误,似乎是在 NVIDIA 驱动程序中(顺便说一句。显卡是 GeForce 105m)。这是我的错还是驱动程序中的错误(我认为是我,因为它上面的每个游戏似乎工作正常)?
这是 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 函数(我不会完成整个代码,因为它太长了;),如果你愿意,我仍然可以 post 它:
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
}
}
这是您的代码中的错误。
您提供给驱动程序的 fileContents
指针完全无效,因此在取消引用此指针时驱动程序崩溃。
您在 C 中没有本机字符串数据类型,您只使用 char
的数组。 C 不会为你做任何类型的内存管理。因此,char 指针上的 += 运算符不执行字符串连接。这只是指针算术。你只是在内存中有一个情感字符串并且 fileContent
最初指向它。通过行
fileContents += symbol;
你将该指针增加 symbol
的数值,因此指向该空字符串之外的一些内存。
我不想听起来很粗鲁,所以请不要误会我的意思。但我真的建议你先学习你想使用的编程语言,然后再继续使用 OpenGL。