J V*_*J V 0 c null segmentation-fault
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$
Run Code Online (Sandbox Code Playgroud)
这是使用-Wall编译并运行程序的输出.不理想.
该错误似乎涉及下面的代码.
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
}
}
Run Code Online (Sandbox Code Playgroud)
它似乎很明显,外环尝试设置cur
到cur->next
时cur == NULL
引起的段错误.是否有可能重建循环以避免这种情况?(我想到了一个do-while循环,但没有成功)
避免这种情况的唯一方法是将语句包含在if语句中吗?
我已经尝试过解决问题的方法.我理解为什么它首先失败了,但鉴于下面的输出它仍然失败:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以printf()
在这附近提供输出吗?即使在故障发生之前,分段故障也会以某种方式阻止其运行.
首先,我稍微修改了缩进并添加了一些注释.
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
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,当if
语句为真时,你运行第二个while循环,它cur
等于NULL
.
随后的取消引用尝试是seg错误.
归档时间: |
|
查看次数: |
434 次 |
最近记录: |