简体   繁体   English

在函数调用(C)上进行段错误

[英]Segfaulting on function call (C)

So, I'm at somewhat of a loss right now and hoping someone can help. 因此,我现在有些茫然,希望有人能提供帮助。 I have a function defined in a library called 'list_free' which, as you can imagine, frees a list that you created earlier. 我在一个名为“ list_free”的库中定义了一个函数,正如您可以想象的那样,该函数将释放您先前创建的列表。 When I call this function from within another module, my program segfaults. 当我从另一个模块中调用此函数时,我的程序出现段错误。 When running GDB though, I get: 当运行GDB时,我得到:

(gdb) bt
#0  *__GI___libc_free (mem=0x65656853) at malloc.c:3709
#1  0x0804f279 in list_free ()
#2  0x0012ffef in set_var (...) at src/calc/model.c:337
#3  0x0804b320 in test_dependency_updates (_i=0) at src/tests/test_calc.c:63
#4  0x080507ea in srunner_run_all ()
#5  0x0804d2b9 in main () at src/tests/test_all.c:19

Thus, the line that caught my attention was #1 ... there is no reference to the source of that function. 因此,引起我注意的那一行是#1 ...没有引用该函数的来源。 When I change the name of the function from list_free to list_freex (ie, just something different) no more segfault. 当我将函数的名称从list_freelist_freex (即,只是有所不同)时,不再出现段错误。 The other strange thing is if I change the name in the .c file, I get no warning of an "undefined reference to list_free" or whatever error I would get from forgetting to link in a library. 另一个奇怪的事情是,如果我更改了.c文件中的名称,则不会收到“未定义对list_free的引用”的警告,也不会因忘记链接到库而产生任何错误。

Thus, I'm led to believe that the function is being defined elsewhere, but I have no idea where. 因此,我被认为该函数是在其他地方定义的,但是我不知道在哪里。 A grep -R list_free . 一个grep -R list_free . offers no help and the gdb output doesn't seem to indicate otherwise. 没有提供帮助,并且gdb输出似乎并未表明其他情况。

So I'm wondering if anyone has any tips on how to hunt down where this function might be defined. 因此,我想知道是否有人对如何确定该函数的定义有任何建议。 Also, I've renamed the function as noted above for now as I don't seem to have any other choice ... but I'm not sure if this is a good/safe idea? 另外,由于我似乎没有其他选择,因此我现在已如上所述重命名了函数……但是我不确定这是否是一个好主意? Ie, could I just be masking other problems by doing so? 即,我可以这样掩盖其他问题吗?

Thanks in advance. 提前致谢。

如果您使用的是linux,则valgrind是查找内存管理问题的宝贵工具。

Try producing a link map from the linker. 尝试从链接器生成链接映射。

On my system, to produce a map on stdout (lots and lots of lines) 在我的系统上,要在stdout上生成地图(很多行)

gcc ... -Wl,-M ...
    ###       ^ lowercase L

Or, to create a file with the map 或者,使用地图创建文件

gcc ... -Wl,-Map,a.map ...

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

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