这是一个困扰我的最小代码:
#include <list>
class A;
class A {
private:
std::list<A*> a_list;
// int a_list;
public:
A() : a_list() {}
};
int main() {
A* a_tab = new A[1];
// A* a_tab = new A();
delete a_tab;
return 0;
}
执行时,我得到了:
*** glibc detected *** ./main: free(): invalid pointer: 0x0000000000f6d018 ***
======= Backtrace: =========
/lib/libc.so.6(+0x774b6)[0x7f706f2914b6]
/lib/libc.so.6(cfree+0x73)[0x7f706f297c83]
./main[0x4007d2]
/lib/libc.so.6(__libc_start_main+0xfe)[0x7f706f238d8e]
./main[0x400649]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:03 108600 /DELL/Users/Lucas/Desktop/Lifebeam/minimal/main
00601000-00602000 r--p 00001000 08:03 108600 /DELL/Users/Lucas/Desktop/Lifebeam/minimal/main
00602000-00603000 rw-p 00002000 08:03 108600 /DELL/Users/Lucas/Desktop/Lifebeam/minimal/main
00f6d000-00f8e000 rw-p 00000000 00:00 0 [heap]
7f7068000000-7f7068021000 rw-p 00000000 00:00 0
7f7068021000-7f706c000000 ---p 00000000 00:00 0
7f706f21a000-7f706f394000 r-xp 00000000 08:07 400996 /lib/libc-2.12.1.so
7f706f394000-7f706f593000 ---p 0017a000 08:07 400996 /lib/libc-2.12.1.so
7f706f593000-7f706f597000 r--p 00179000 08:07 400996 /lib/libc-2.12.1.so
7f706f597000-7f706f598000 rw-p 0017d000 08:07 400996 /lib/libc-2.12.1.so
7f706f598000-7f706f59d000 rw-p 00000000 00:00 0
7f706f59d000-7f706f5b2000 r-xp 00000000 08:07 391760 /lib/libgcc_s.so.1
7f706f5b2000-7f706f7b1000 ---p 00015000 08:07 391760 /lib/libgcc_s.so.1
7f706f7b1000-7f706f7b2000 r--p 00014000 08:07 391760 /lib/libgcc_s.so.1
7f706f7b2000-7f706f7b3000 rw-p 00015000 08:07 391760 /lib/libgcc_s.so.1
7f706f7b3000-7f706f835000 r-xp 00000000 08:07 401151 /lib/libm-2.12.1.so
7f706f835000-7f706fa34000 ---p 00082000 08:07 401151 /lib/libm-2.12.1.so
7f706fa34000-7f706fa35000 r--p 00081000 08:07 401151 /lib/libm-2.12.1.so
7f706fa35000-7f706fa36000 rw-p 00082000 08:07 401151 /lib/libm-2.12.1.so
7f706fa36000-7f706fb1e000 r-xp 00000000 08:07 1048147 /usr/lib/libstdc++.so.6.0.14
7f706fb1e000-7f706fd1d000 ---p 000e8000 08:07 1048147 /usr/lib/libstdc++.so.6.0.14
7f706fd1d000-7f706fd25000 r--p 000e7000 08:07 1048147 /usr/lib/libstdc++.so.6.0.14
7f706fd25000-7f706fd27000 rw-p 000ef000 08:07 1048147 /usr/lib/libstdc++.so.6.0.14
7f706fd27000-7f706fd3c000 rw-p 00000000 00:00 0
7f706fd3c000-7f706fd5c000 r-xp 00000000 08:07 401152 /lib/ld-2.12.1.so
7f706ff3b000-7f706ff40000 rw-p 00000000 00:00 0
7f706ff5a000-7f706ff5c000 rw-p 00000000 00:00 0
7f706ff5c000-7f706ff5d000 r--p 00020000 08:07 401152 /lib/ld-2.12.1.so
7f706ff5d000-7f706ff5e000 rw-p 00021000 08:07 401152 /lib/ld-2.12.1.so
7f706ff5e000-7f706ff5f000 rw-p 00000000 00:00 0
7fffdf34a000-7fffdf36b000 rw-p 00000000 00:00 0 [stack]
7fffdf3da000-7fffdf3db000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Abandon
有两条注释行:它们(独立地)是一种替代方案,可以在它们之前的行中正常工作。
你是否有任何线索为何这样做?
我用 g ++编译(Ubuntu / Linaro 4.4.4-14ubuntu5)4.4.5 。
答案 0 :(得分:6)
您使用了错误的删除。您已使用运算符new[]
分配了一个元素的数组,但您使用运算符delete
将其作为单个对象删除。请改用delete[] a_tab;
。