GCC:优化时分段故障和调试程序,只有崩溃 [英] GCC: Segmentation fault and debugging program that only crashes when optimized
问题描述
这是一个跟进主题:<一href=\"http://stackoverflow.com/questions/22828609/c-segmentation-fault-and-maybe-gdb-is-lying-to-me/22829842?noredirect=1#22829842\">C:分段错误,也许是GDB对我说谎
我有一个用-O0但段错误编译罚款与-O1,-O2,-O3和Ofast程序。这似乎堆栈被莫名其妙地在某一点损坏,但我不明白,为什么或者
I have a program that compiles fine with -O0 but segfaults with -O1, -O2, -O3 and Ofast. It seems the stack somehow gets corrupted at some point but I can't figure out why or where.
首先,这里是我使用该结构的一部分。这是一个头文件:
First, here's part of the struct that I'm using. It's in a header file:
typedef struct {
GLuint nsHandle;
} OGL_STATE_T;
下面是主文件的相关冲淡部分:
Here's the relevant watered down part of the main file:
void init(OGL_STATE_T *state) {
printf("init: %p\n", state); // Print pointer address make sure it's the same.
compileShaders(state);
}
int main(argc, char *argv[]) {
static OGL_STATE_T _state, *state=&_state;
printf("main: %p\n", state); // Print pointer address
init(state);
return 0;
}
然后这里的compileShaders功能。这就是指针地址腐败发生的:
Then here's the compileShaders function. This is where the pointer address corruption happens:
void compileShaders(OGL_STATE_T *state) {
printf("compileShaders entry: %p\n", state); // Print pointer address make sure it's good
GLuint nsVertex = compileShader("nsVertex", GL_VERTEX_SHADER, state);
GLuint nsFragment = compileShader("nsFragment", GL_FRAGMENT_SHADER, state);
printf("compileShaders return: %p\n", state); // Print pointer when returning.
state->nsHandle = glCreateProgram(); // Segmentation fault here.
/* ... */
}
这将在输出来说明这里不久,第二个printf语句返回错误的地址。
As will be illustrated in the output here shortly, the second printf statement returns the wrong address.
最后,compileShader功能(注意:在名称末尾缺少的'在这里)。本来功能没有采取状态指针,但我说,所以我可以跟踪,其中在执行腐败发生。
Finally, the compileShader function (Notice the lack of 's' at the end in the name here). Originally the function didn't take the state pointer but I added it so I could track where in the execution the corruption happens.
GLuint compileShader{char * shaderName, GLenum shaderType, OGL_STATE_T *state} {
printf("compileShader 1: %p\n", state); // Print state address at function entry
FILE *shaderFile;
char fileName[sizeof shaderName + 8];
long lSize;
char *buffer;
strcpy(fileName, "./");
strcpy(fileName, shaderName);
strcpy(fileName, ".glsl");
shaderFile = fopen(fileName, "rb");
fseek(shaderFile, 0L, SEEK_END);
lSize = ftell(shaderFile);
rewind(shaderFile);
buffer = calloc(1, lSize + 1);
GLuint shaderHandle = glCreateShader(shaderType);
printf("compileShader 2: %p\n", state); // Print state address at function middle
const GLchar *shaderString = buffer;
glShaderSource(shaderHandle, 1, &shaderString, 0);
glCompileShader(shaderHandle);
GLint compileSuccess;
glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &compileSuccess);
fclose(shaderFile);
shaderString = NULL;
free(buffer);
printf("compileShader 3: %p\n\n", state); // Print state address before returning
return shaderHandle;
}
现在这里最厉害的是输出:
Now for the kicker here's the output:
main: 0x1387c
init: 0x1387c
compileShaders entry: 0x1387c
compileShader 1: 0x1387c
compileShader 2: 0x1387c
compileShader 3: 0x1387c
compileShader 1: 0x1387c
compileShader 2: 0x1387c
compileShader 3: 0x1387c
compileShaders return: 0x1006c
Segmentation fault. (Core dumped)
因此,这是告诉我,当compileShader()函数退出地址仍然是良好的,并返回到compileShaders后()(请不要混淆人有一个S,另一个没有),地址被破坏?
So this is telling me that when the compileShader() function exits the address is still good and after it returns into compileShaders() (Please do not get confused one has a 's' and the other one doesn't), the address gets corrupted?
我有点在这一点大吃一惊。这是很难调试这是因为如果我不优化code我没有得到任何错误。但是,如果我做的优化code(如果它是O1,O2,O3或Ofast并不重要)我得到段错误。
I'm a bit flabbergasted at this point. It's hard to debug this because if I don't optimize the code I do not get any errors. However if I do optimize the code (Doesn't matter if it's O1, O2, O3 or Ofast) I get the segmentation fault.
的printf的语句是我唯一的朋友,现在和他们没有告诉我,在这一点上任何东西。这是我提交错误报告,海湾合作委员会的部分?
The printf statements are my only friend right now and they're not telling me anything at this point. Is this the part where I submit a bug report to GCC?
由于任何人谁需要时间来阅读这个线程。我知道这是对事物的长边位。
Thanks to anyone who takes the time to read this thread. I know it's a bit on the longer side of things.
推荐答案
问题是在 compileShaders()的
文件名
定义
Problem is with the fileName
definition in the compileShaders()
char fileName[sizeof shaderName + 8]; .
这是不正确的,而不是为文件名
分配足够的字节。
That is not correct and not allocate enough bytes for fileName
.
您需要使用来分配的strlen(shaderName)+8
,而不是的sizeof(shaderName)
。
这篇关于GCC:优化时分段故障和调试程序,只有崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!