C++ glibc 检测到损坏的双链表错误 C++

C++ glibc detected corrupted double-linked list error C++

我在 Population.cpp 中有以下二维动态整数链表的声明:

sectionProf = new int*[section_count]; //list of professor for each section declaration

在Population.h中定义为:

int ** sectionProf; //list of professor for each section

然后在 Population.cpp 中再次从文件中填充它,稍后:

sectionProf[section] = new int[professors + 1];
sectionProf[section][0] = professors;
if (professors > 0) {
    for (int x = 1; x < professors + 1; ++x) {
        sectionProf[section][x] = stoi(tokenizedVersion[x + 1]);
    }
}

然后,在析构函数中,我按如下方式销毁它:

if(sectionProf){
    for(int i = 0; i < section_count; ++i){
        delete [] sectionProf[i];
    }
    delete [] sectionProf;
}

但是,在执行时,我不断收到以下错误:

* glibc detected * ./research_scheduling_backend: corrupted double-linked list: 0x00000000020b78c0 ***

这是 gdb 回溯(#17 指的是 'delete [] sectionProf' 行):

#0  __lll_lock_wait_private () at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:93
#1  0x00007ffff7085f61 in _L_lock_10611 () at malloc.c:5249
#2  0x00007ffff7083c87 in __GI___libc_malloc (bytes=140737341265696) at malloc.c:2921
#3  0x00007ffff7de7900 in _dl_map_object_deps (map=0x7ffff7fdd4e0, preloads=<optimized out>, npreloads=<optimized out>, trace_mode=0, open_mode=-2147483648) at dl-deps.c:517
#4  0x00007ffff7ded8a9 in dl_open_worker (a=0x7fffffffbb00) at dl-open.c:262
#5  0x00007ffff7de9176 in _dl_catch_error (objname=0x7fffffffbb48, errstring=0x7fffffffbb50, mallocedp=0x7fffffffbb5f, operate=0x7ffff7ded700 <dl_open_worker>, args=0x7fffffffbb00) at dl-error.c:178
#6  0x00007ffff7ded31a in _dl_open (file=0x7ffff717a858 "libgcc_s.so.1", mode=-2147483647, caller_dlopen=0x7ffff710bea5, nsid=-2, argc=3, argv=<optimized out>, env=0x7fffffffeac8) at dl-open.c:639
#7  0x00007ffff7131bb2 in do_dlopen (ptr=0x7fffffffbd00) at dl-libc.c:89
#8  0x00007ffff7de9176 in _dl_catch_error (objname=0x7fffffffbd30, errstring=0x7fffffffbd20, mallocedp=0x7fffffffbd3f, operate=0x7ffff7131b70 <do_dlopen>, args=0x7fffffffbd00) at dl-error.c:178
#9  0x00007ffff7131c74 in dlerror_run (args=0x7fffffffbd00, operate=0x7ffff7131b70 <do_dlopen>) at dl-libc.c:48
#10 __GI___libc_dlopen_mode (name=<optimized out>, mode=<optimized out>) at dl-libc.c:165
#11 0x00007ffff710bea5 in init () at ../sysdeps/x86_64/../ia64/backtrace.c:53
#12 0x00007ffff6df1400 in pthread_once () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_once.S:104
#13 0x00007ffff710bfc4 in __GI___backtrace (array=<optimized out>, size=64) at ../sysdeps/x86_64/../ia64/backtrace.c:104
#14 0x00007ffff707505f in __libc_message (do_abort=2, fmt=0x7ffff717f560 "*** glibc detected *** %s: %s: 0x%s ***\n") at ../sysdeps/unix/sysv/linux/libc_fatal.c:180
#15 0x00007ffff707f846 in malloc_printerr (action=3, str=0x7ffff717be4c "corrupted double-linked list", ptr=<optimized out>) at malloc.c:5047
#16 0x00007ffff7080b1b in _int_free (av=0x7ffff73b9720, p=0x627dd0, have_lock=0) at malloc.c:4125
#17 0x0000000000404b7e in Population::~Population (this=0x7fffffffc910, __in_chrg=<optimized out>) at Population.cpp:91
#18 0x0000000000403919 in main (argc=3, argv=0x7fffffffeaa8) at Scheduler.cpp:101

代码中绝对没有任何地方修改过 sectionProf 数组。它仅用于检查值。有人可以告诉我为什么我会收到这个错误吗?我到处查看有关 glibc 双链表错误的地方,我知道这是因为我以某种方式破坏了符号 table(?) 不知何故...

对于遇到此问题的任何人,这是我的特定问题的错误所在。当我生成数组时,我正在读取超出范围 (section_count) 的部分索引的垃圾值。即在for循环中,

sectionProf[section] = new int[professors + 1];
sectionProf[section][0] = professors;
if (professors > 0) {
    for (int x = 1; x < professors + 1; ++x) {
        sectionProf[section][x] = stoi(tokenizedVersion[x + 1]);
    }
}

我的 section 值不在 0section_count 的范围内,删除循环中使用的索引。这就是我造成内存损坏的原因。

课程:检查输入文件中是否产生了 PEBKAC 错误。