简体   繁体   English

分段错误,我不知道是什么原因造成的

[英]Segmentation fault and I don't know what's causing it

main.c:132:26: warning: "/*" within comment
main.c: In function ‘importSettings’:
main.c:152: warning: control reaches end of non-void function
main.c: In function ‘cs_init_missions’:
main.c:84: warning: control reaches end of non-void function
j@jonux:~/Projects/csgtk/c$ ./a.out 
Segmentation fault
j@jonux:~/Projects/csgtk/c$ 

This is the output from a compile with -Wall and running the program.这是使用 -Wall 编译并运行程序的 output。 Not ideal.不理想。

The error seems to involve the code below.该错误似乎涉及下面的代码。

static xmlDocPtr importSettings(char file[], GtkBuilder *builder) {
    cur = cur->xmlChildrenNode;
    while (cur != NULL) {
        if(xmlStrEqual(cur->name, (const xmlChar *) "options")) {
            cur = cur->xmlChildrenNode;
            while (cur != NULL) {
                cur = cur->next; 
            }
        }
        cur = cur->next;// <- Segfault is here
    }
}

It seems obvious that the outer loop attempting to set cur to cur->next when cur == NULL is causing the segfault.似乎很明显,当cur == NULL时,试图将cur设置为cur->next的外部循环导致了段错误。 Is it possible to reconstruct the loop to avoid this?是否可以重建循环以避免这种情况? (I thought of a do-while loop but that didn't pan out) (我想到了一个 do-while 循环,但没有成功)

Is the only way to avoid this to encase the statement in an if statement?避免这种情况的唯一方法是将语句包含在 if 语句中吗?

I have tried something to fix the problem.我已经尝试了一些方法来解决这个问题。 I understand why it was failing in the first place, but it is still failing given the output below:我理解为什么它首先失败了,但是考虑到下面的 output,它仍然失败:

static xmlDocPtr importSettings(char file[], GtkBuilder *builder){
        if (file == NULL) {
            file = "CsSettings.xml";
        }
        //commented stuff here
        settingsTree = xmlParseFile(file);
        //commented stuff here
        cur = xmlDocGetRootElement(settingsTree);
        cur = cur->xmlChildrenNode;
        while (cur != NULL){

            if(xmlStrEqual(cur->name, (const xmlChar *) "options")){ 
                cur = cur->xmlChildrenNode;
                while (cur != NULL){
        //commented stuff here
                    if(cur->next == NULL)
                        cur = cur->parent;
                    else
                        cur = cur->next;

                }
            }
            cur = cur->next;
        }
}

Is there any way to get printf() to give output near here?有什么方法可以让printf()在附近给 output 吗? The segmentation fault is somehow stopping it from running even before the fault occurs.甚至在故障发生之前,分段错误就以某种方式阻止它运行。

First of all I fixed your indentation a little and added some annotations.首先,我稍微修正了您的缩进并添加了一些注释。

static xmlDocPtr importSettings(char file[], GtkBuilder *builder){
    cur = cur->xmlChildrenNode;
    while (cur != NULL){
        if(xmlStrEqual(cur->name, (const xmlChar *) "options")){
            cur = cur->xmlChildrenNode;
            while (cur != NULL){
                cur = cur->next;
            }
            //at this point, cur == NULL
        }
        cur = cur->next;//seg fault here
    }
}

The problem is that when the if statement is true you run a second while loop which leaves cur equal to NULL .问题是,当if语句为真时,您运行第二个 while 循环,使cur等于NULL

The subsequent attempt to de-reference is the seg fault.随后的取消引用尝试是段错误。

The problem is likely to be at the 2nd cur = cur->next;问题很可能出在第二个cur = cur->next; , which follows the while loop - at that point the loop has guaranteed that cur == NULL . ,在while循环之后 - 在这一点上,循环保证cur == NULL

I'm not sure what you're trying to do in that function (has some code been elided for the example?), so don't really have a suggesting at the moment.我不确定您在 function 中要做什么(示例中是否省略了一些代码?),所以目前真的没有建议。

Also, as your compiler warnings indicate, nothing is being returned by the function even though it's declared to do so.此外,正如您的编译器警告所示,function 没有返回任何内容,即使它已声明这样做。 I would have assumed again that this is because something is removed for the purposes of the example in the question, but then again your compiler is complaining as well.我会再次假设这是因为出于问题示例的目的而删除了某些内容,但是您的编译器也再次抱怨。 Any caller that uses whatever the function is supposed to return is in for a surprise.任何使用 function 应该返回的调用者都会感到惊讶。 That surprise may also be a segfault.这个惊喜也可能是一个段错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM