“free():无效指针”错误和核心转储

时间:2013-05-14 12:57:11

标签: c free coredump

我在程序成功执行结束时获得了这个不需要的输出,同时释放了动态分配的动态内存。

*** glibc detected *** /home/ahor/Desktop/Project Work/node: free(): invalid pointer: 0xb7fda000 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb7e93ee2]
/home/ahor/Desktop/Project Work/node[0x8048cf9]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb7e374d3]
/home/ahor/Desktop/Project Work/node[0x8048561]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:06 786473     /home/ahor/Desktop/Project Work/node
0804a000-0804b000 r--p 00001000 08:06 786473     /home/ahor/Desktop/Project Work/node
0804b000-0804c000 rw-p 00002000 08:06 786473     /home/ahor/Desktop/Project Work/node
0804c000-0806d000 rw-p 00000000 00:00 0          [heap]
b7deb000-b7e07000 r-xp 00000000 08:06 2228253    /lib/i386-linux-gnu/libgcc_s.so.1
b7e07000-b7e08000 r--p 0001b000 08:06 2228253    /lib/i386-linux-gnu/libgcc_s.so.1
b7e08000-b7e09000 rw-p 0001c000 08:06 2228253    /lib/i386-linux-gnu/libgcc_s.so.1
b7e1d000-b7e1e000 rw-p 00000000 00:00 0 
b7e1e000-b7fc1000 r-xp 00000000 08:06 2228351    /lib/i386-linux-gnu/libc-2.15.so
b7fc1000-b7fc3000 r--p 001a3000 08:06 2228351    /lib/i386-linux-gnu/libc-2.15.so
b7fc3000-b7fc4000 rw-p 001a5000 08:06 2228351    /lib/i386-linux-gnu/libc-2.15.so
b7fc4000-b7fc7000 rw-p 00000000 00:00 0 
b7fd7000-b7fd8000 rw-p 00000000 00:00 0 
b7fd8000-b7fd9000 rw-s 00000000 00:04 12517386   /SYSV001120bd (deleted)
b7fd9000-b7fda000 rw-p 00000000 00:00 0 
b7fda000-b7fdb000 rw-s 00000000 00:04 12484617   /SYSV00307eff (deleted)
b7fdb000-b7fdd000 rw-p 00000000 00:00 0 
b7fdd000-b7fde000 r-xp 00000000 00:00 0          [vdso]
b7fde000-b7ffe000 r-xp 00000000 08:06 2228363    /lib/i386-linux-gnu/ld-2.15.so
b7ffe000-b7fff000 r--p 0001f000 08:06 2228363    /lib/i386-linux-gnu/ld-2.15.so
b7fff000-b8000000 rw-p 00020000 08:06 2228363    /lib/i386-linux-gnu/ld-2.15.so
bffdf000-c0000000 rw-p 00000000 00:00 0          [stack]

......
......
......

当我使用gdb回溯时,我收到此消息。

Program received signal SIGABRT, Aborted.
0xb7fdd424 in __kernel_vsyscall ()
(gdb) bt
#0  0xb7fdd424 in __kernel_vsyscall ()
#1  0xb7e4c1df in raise () from /lib/i386-linux-gnu/libc.so.6
#2  0xb7e4f825 in abort () from /lib/i386-linux-gnu/libc.so.6
#3  0xb7e8939a in ?? () from /lib/i386-linux-gnu/libc.so.6
#4  0xb7e93ee2 in ?? () from /lib/i386-linux-gnu/libc.so.6
#5  0x08048cf9 in main (argc=6, argv=0xbffff214) at node.c:180

...... ...... 这是影响我的代码段吗?我无法察觉原因,如果你能帮助我,我将非常感激。

// creating the dynamic memory for net
    if((net = (struct dot **)malloc(v * sizeof(struct dot))) == NULL) {
        perror("struct dot malloc() failed\n");
        exit(1);
    }
    for(i = 0 ; i < v; i++)
        if((net[i] = (struct dot *)malloc(v * sizeof(struct dot))) == NULL) {
        perror("net[i] malloc() failed.\n");
        for(j = 0; j < i; j++)
            free(net[j]);
        free(net);      
        }
.....
.....
// creating the dynamic memory for link_status
    if((link_status = (struct link **)malloc(v * sizeof(struct link))) == NULL) {
        perror("struct link malloc() failed\n");
        exit(1);
    }
    for(i = 0 ; i < v; i++)
        if((link_status[i] = (struct link *)malloc(v * sizeof(struct link))) == NULL) {
        perror("link_status[i] malloc() failed.\n");
        for(j = 0; j < i; j++)
            free(link_status[j]);
        free(link_status);      
        }
......

检测到的glibc来自下面使用的free()函数。

// free the allocated dynamic memories
    for(i = 0; i < v; i++) {
        free(net[i]);
        free(link_status[i]);
    }
    free(net);
    free(link_status);

请帮助我!

1 个答案:

答案 0 :(得分:1)

你正在覆盖一些不属于你的记忆。您可以使用valgrind查找负责写入错误的代码。