解释gdb分段错误

时间:2017-07-17 06:05:01

标签: c segmentation-fault gdb valgrind

我用谷歌搜索,用谷歌搜索,用魔法搜索: - (

raspian

如果我在gdb下运行我的程序,我得到:

(gdb) run
Starting program: /home/pi/axcept/a 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/arm-linux-
gnueabihf/libthread_db.so.1".
axcept v:1.0 20170710
running
[New Thread 0x768e3450 (LWP 6671)]
[Thread 0x768e3450 (LWP 6671) exited]
*** Error in `/home/pi/axcept/a': double free or corruption (out): 
0x00025178 ***

Program received signal SIGABRT, Aborted.
0x76a1af70 in __GI_raise (sig=sig@entry=6) at 
../nptl/sysdeps/unix/sysv/linux/raise.c:56
56  ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or 
directory.
(gdb) where
#0  0x76a1af70 in __GI_raise (sig=sig@entry=6) at 
../nptl/sysdeps/unix/sysv/linux/raise.c:56
#1  0x76a1c324 in __GI_abort () at abort.c:89
#2  0x76a56954 in __libc_message (do_abort=<optimized out>, 
fmt=0x76b0c6e8 "*** Error in `%s': %s: 0x%s ***\n") at 
../sysdeps/posix/libc_fatal.c:175
#3  0x76a5cb80 in malloc_printerr (action=1, str=0x76b0c860 "double 
free or corruption (out)", ptr=<optimized out>) at malloc.c:4996
#4  0x76a5db24 in _int_free (av=<optimized out>, p=<optimized out>, 
have_lock=1992996040) at malloc.c:3840
#5  0x76a7d72c in tzset_internal (always=0, explicit=1991424272) at 
tzset.c:443
#6  0x76a7db5c in __tz_convert (timer=0x7efff1e0, use_localtime=1, 
tp=0x76b2d2cc <_tmbuf>) at tzset.c:632
#7  0x00011190 in get_today () at today.c:17
#8  0x00012f14 in main (argc=1, argv=0x7efff374) at axcept.c:333
(gdb) list
51  in ../nptl/sysdeps/unix/sysv/linux/raise.c
(gdb) 

在axcept.c中,我有333左右:

...
initialise_db();
read_parameters(0);
get_today();
....

但read_parameters和get_today都或多或少地从官方gnu示例中被盗。

来自我的makefile:

CC=gcc 
CCFLAGS=  -fgnu89-inline -g -v -da -Q -std=c99 -I/usr/local/include -
L/usr/local/lib -lwiringPi -I/usr/include/mysql/ -I/home/pi/logging/  
`mysql_config --cflags --libs` -lwiringPi -lwiringPiDev -lpthread -lm 
-lcrypt -lrt

我尝试了valgrind -v --track-originins = yes myprog,但它没有让我更聪明。

现在喝醉了!

1 个答案:

答案 0 :(得分:3)

双重免费或腐败意味着您已经free两次,或某些事情覆盖了malloc对象之外的簿记。问题可以在以后显现出来 - 这里malloc注意到了问题,因此实际的堆损坏发生在之前。

如果valgrind似乎没有帮助(好吧,它应该),请尝试通过用无用的malloc替换get_today()调用来复制此错误;如果它工作,那么你可以尝试通过移动和删除部分代码来跟踪代码,直到你有一个MCVE(可能会自行解决)。