C:分段故障,也许是GDB对我说谎 [英] C: Segmentation fault and maybe GDB is lying to me

查看:121
本文介绍了C:分段故障,也许是GDB对我说谎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是出现segfaults C函数:

Here is a C function that segfaults:

void compileShaders(OGL_STATE_T *state) {

    // First testing to see if I can access object properly. Correctly outputs:
    // nsHandle: 6
    state->nsHandle = 6;
    printf("nsHandle: %d\n", state->nsHandle);

    // Next testing if glCreateProgram() returns proper value. Correctly outputs:
    // glCreateProgram: 1
    printf("glCreateProgram: %d\n", glCreateProgram());


    // Then the program segfaults on the following line according to gdb
    state->nsHandle = glCreateProgram();

}

有关记录语句> nsHandle的类型GLuint和glCreateProgram()的返回一个GLuint,这样不应该是我的问题。

For the record state->nsHandle is of type GLuint and glCreateProgram() returns a GLuint so that shouldn't be my problem.

gdb的说,我的程序就行303出现segfaults这实际上是该行前的注释行。我不知道,其实很重要。

gdb says that my program segfaults on line 303 which is actually the comment line before that line. I don't know if that actually matters.

时GDB骗我?如何调试呢?

Is gdb lying to me? How do I debug this?

编辑:

熄灭优化(-O3),现在它的工作。如果有人可以解释为什么这将是巨大的,但。

Turned off optimizations (-O3) and now it's working. If somebody could explain why that would be great though.

编辑2:
对于意见的目的,这里的重要组成部分,一个淡化版本:

EDIT 2: For the purpose of the comments, here's a watered down version of the important components:

typedef struct {
    GLuint nsHandle;
} OGL_STATE_T;

int main (int argc, char *argv[]) {
    OGL_STATE_T _state, *state=&_state;
    compileShaders(state);
}

修改3:
这里有一个测试,我所做的:

EDIT 3: Here's a test I did:

int main(int argc, char *argv[]) {

    OGL_STATE_T _state, *state=&_state;

    // Assign value and try to print it in other function
    state->nsHandle = 5;

    compileShaders(state);

}

void compileShaders(OGL_STATE_T *state) {

    // Test to see if the first call to state is getting optimized out
    // Correctly outputs:
    // nsHandle (At entry): 5
    printf("nsHandle (At entry): %d\n", state->nsHandle);

}

不知道是否可以帮助任何东西,或如果编译器实际上从main函数优化价值。

Not sure if that helps anything or if the compiler would actually optimize the value from the main function.

修改4:
在主compileShaders,一切比赛打印出指针地址。所以我要承担起它的其他段错​​误的地方和gdb是在骗我一下哪一行实际上是导致它。

EDIT 4: Printed out pointer address in main and compileShaders and everything matches. So I'm gonna assume it's segfaulting somewhere else and gdb is lying to me about which line is actually causing it.

推荐答案

我最终开始一个新的线程有更多相关的信息,有人找到了答案。新线程是在这里:

I ended up starting a new thread with more relevant information and somebody found the answer. New thread is here:

<一个href=\"http://stackoverflow.com/questions/22876433/gcc-segmentation-fault-and-debugging-program-that-only-crashes-when-optimized/22876481#22876481\">GCC:分段故障和调试程序,只有当优化崩溃

这篇关于C:分段故障,也许是GDB对我说谎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆