一、报错信息
[2023-7]--[ Debug ]--Destroy DqlResult Struct OK
[2023-7]--[ Debug ]--Destroy Moia Base Job : OK
[2023-7]--[ Debug ]--Destroy Moia Base Job : OK
[2023-7]--[ Debug ]--Destroy Moia Base Job : OK
[2023-7]--[ Debug ]--Destroy Moia Job : OK
[2023-7]--[ Debug ]--Destroy Moia Base Job : OK
[2023-7]--[ Debug ]--Destroy Moia Base Job : OK
[2023-7]--[ Debug ]--Destroy Moia Base Job : OK
[2023-7]--[ Debug ]--Destroy Moia Job : OK
[2023-7]--[ Debug ]--Destroy Moia Base Job : OK
*** Error in `../ExecuteFile/TestMoiaTableCode': munmap_chunk(): invalid pointer: 0x0000000002070150 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x7f474)[0x7ff60d7dd474]
../ExecuteFile/TestMoiaTableCode[0x402aec]
../ExecuteFile/TestMoiaTableCode[0x402e0c]
../ExecuteFile/TestMoiaTableCode[0x402f9d]
../ExecuteFile/TestMoiaTableCode[0x4050bf]
../ExecuteFile/TestMoiaTableCode[0x4050e0]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7ff60d780555]
../ExecuteFile/TestMoiaTableCode[0x400e79]
======= Memory map: ========
00400000-00407000 r-xp 00000000 fd:00 4662 /opt/Developer/Moia/ExecuteFile/TestMoiaTableCode
00606000-00607000 r--p 00006000 fd:00 4662 /opt/Developer/Moia/ExecuteFile/TestMoiaTableCode已放弃(吐核)
我在原有正常代码的基础上添加了一段逻辑,再跑,在释放内存时,出现了上述错误。
查看网上的这个错误解释:程序试图释放一个无效的内存地址。会出现此错误。
看完还是一脸懵。
二、GDB调试
[gbase@czg2 Src]$ gdb ../ExecuteFile/TestMoiaTableCode core.29250
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /opt/Developer/Moia/ExecuteFile/TestMoiaTableCode...done.
warning: core file may not match specified executable file.
[New LWP 29250]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Core was generated by `../ExecuteFile/TestMoiaTableCode'.
Program terminated with signal 6, Aborted.
#0 0x00007ff60d794387 in raise () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_9.x86_64 libgcc-4.8.5-44.el7.x86_64 sssd-client-1.16.5-10.el7_9.13.x86_64
(gdb)
(gdb) bt
#0 0x00007ff60d794387 in raise () from /lib64/libc.so.6
#1 0x00007ff60d795a78 in abort () from /lib64/libc.so.6
#2 0x00007ff60d7d6f67 in __libc_message () from /lib64/libc.so.6
#3 0x00007ff60d7dd474 in malloc_printerr () from /lib64/libc.so.6
#4 0x0000000000402aec in DestroyMoiaBaseJob (MBJ=0x206c138) at MoiaTableCode.c:59
#5 0x0000000000402e0c in DestroyMoiaJob (MJ=0x206c130) at MoiaTableCode.c:115
#6 0x0000000000402f9d in DestroyMoiaTableSets (MTS=0x7ffed9acffd8) at MoiaTableCode.c:149
#7 0x00000000004050bf in CleanConfigTableData () at DbOperate.c:50
#8 0x00000000004050e0 in main () at main.c:15
(gdb) f 4
#4 0x0000000000402aec in DestroyMoiaBaseJob (MBJ=0x206c138) at MoiaTableCode.c:59
59 free((*MBJ)->ExecuteCalendar);
(gdb) quit
看上图中的报错处,看不出什么问题,因为在加新逻辑之前是正常销毁的,这时想到报错是否和之前写那段逻辑有关。
三、问题原因
1、重复的变量名
图中画红框的两个变量原来是一样的名字,后来改成不一样的名字了,但奇怪的是我在编译的时候,没有提示任何报错,gcc编译时加的是-Wall -Wextra参数,我以后注意。
总结:由于一个函数中定义了两个相同名称的变量,其中一个变量是malloc的,将其释放,会报此错误。
2、字符串A复制超过定义长度的字符串B
MBJ->DependentEvent:长度为100的char数组。
ValStr->String:长度为105的char数组。
而且还复制字符串和写数据成功了,苦笑,这个错误我是用二分法来定位的。
将MBJ->DependentEvent的定义长度加长,此问题解决。
总结:例如字符串定义长度为100(malloc的变量),使用strcpy、sprintf进行字符串复制一个超过100字节的数据,最后释放此变量也会出现此问题。