在nvidia-340-updates中获取分段错误

时间:2015-07-04 19:08:58

标签: c linux opengl glsl nvidia

我有点学习SDL OpenGL for C ++(我的错误),我不得不把它移植到C.因为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函数(我不会完成整个代码,因为它太长了;),如果你愿意,我仍然可以发布它:)

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
    }
}

1 个答案:

答案 0 :(得分:3)

这是您的代码中的错误。

您给驱动程序的fileContents指针完全无效,因此驱动程序在解除引用此指针时崩溃。

您在C中没有本机字符串数据类型,您只需使用char数组。而且C不会为你做任何类型的内存管理。因此,char指针上的+ =运算符不执行字符串连接。这只是指针算术。你在内存中只有一个情绪化的字符串,fileContent最初指向它。按行

fileContents += symbol;

你将指针增加symbol的数值,因此指向超出该空字符串的一些内存。

我不会听起来粗糙,所以请不要误解我的意思。但我真的建议您在继续使用OpenGL之前先学习一下您想要使用的编程语言。