11

I am getting the following type of error. I know it has something to do with me improperly accessing memory, but I don't exactly how. Please help me see where I have gone wrong.

*note I have simplified my function and it is not obvious what the variables are doing, I just need to know how I am implementing the function incorrectly or where I am misusing memory access.

int my_function(char const *file_name, size_t max)
        {

        myStruct.pStore = fopen(file_name,"w+");      //pStore is a FILE* 
        myStruct.max = max;                 

        // fill the with zeros ('0')
        int numberOfZeros = max*SIZE;
        char zeros[numberOfZeros];                      

        int i=0;
        while(i<numberOfZeros)         // insert zero's 
        {
                zeros[i]='0';
                i++;
        }
        fwrite(zeros,sizeof(char),numberOfZeros,myStruct.pStore);
        fclose(myStruct.pStore);

        return EXIT_SUCCESS; 

The error I am given:

*** glibc detected *** /home/.../: double free or corruption (top): 0x0804c008 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x73e42)[0xb7e82e42]
/lib/i386-linux-gnu/libc.so.6(fclose+0x154)[0xb7e72384]
/home/2012/spatar/cs/specs/release[0x80486b0]
/home/2012/spatar/cs/specs/release[0x8048acd]
/home/2012/spatar/cs/specs/release[0x8048af0]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb7e284d3]
/home/2012/spatar/cs/specs/release[0x80484e1]
 ======= Memory map: ========
08048000-0804a000 r-xp 00000000 00:3b 2331829    /home/2012/spatar/cs/Aspecs/release
0804a000-0804b000 r--p 00001000 00:3b 2331829    /home/2012/spatar/cs/specs/release
0804b000-0804c000 rw-p 00002000 00:3b 2331829    /home/2012/spatar/cs/specs/release
0804c000-0806d000 rw-p 00000000 00:00 0          [heap]
b7e0e000-b7e0f000 rw-p 00000000 00:00 0 
b7e0f000-b7fae000 r-xp 00000000 00:11 5415       /lib/i386-linux-gnu/libc-2.15.so
b7fae000-b7fb0000 r--p 0019f000 00:11 5415       /lib/i386-linux-gnu/libc-2.15.so
b7fb0000-b7fb1000 rw-p 001a1000 00:11 5415       /lib/i386-linux-gnu/libc-2.15.so
b7fb1000-b7fb4000 rw-p 00000000 00:00 0 
b7fbc000-b7fd8000 r-xp 00000000 00:11 5426       /lib/i386-linux-gnu/libgcc_s.so.1
b7fd8000-b7fd9000 r--p 0001b000 00:11 5426       /lib/i386-linux-gnu/libgcc_s.so.1
b7fd9000-b7fda000 rw-p 0001c000 00:11 5426       /lib/i386-linux-gnu/libgcc_s.so.1
b7fda000-b7fdd000 rw-p 00000000 00:00 0 
b7fdd000-b7fde000 r-xp 00000000 00:00 0          [vdso]
b7fde000-b7ffe000 r-xp 00000000 00:11 5405       /lib/i386-linux-gnu/ld-2.15.so
b7ffe000-b7fff000 r--p 0001f000 00:11 5405       /lib/i386-linux-gnu/ld-2.15.so
b7fff000-b8000000 rw-p 00020000 00:11 5405       /lib/i386-linux-gnu/ld-2.15.so
bffdf000-c0000000 rw-p 00000000 00:00 0          [stack]
4

2 回答 2

10

看起来您正在尝试释放已释放或取消引用的内存。

将您的程序与efence链接或使用valgrind运行它。

这将告诉您指针在何处被取消引用。

于 2012-09-23T01:01:52.553 回答
6

内存损坏通常是由超出分配内存末尾的写入引起的,并且通常是一个字节,因为有人忘记添加空值终止字符串所需的一个字节。

Double free 意味着 free(x) 连续调用两次,x 的值相同。在您的代码中某处 free(x) 被调用,然后很可能在另一段代码中 free(x) 再次被调用。

隔离问题的最简单方法是使用 gdb 并在单步执行代码时观察正在发生的情况。

在上面的 my_function 代码中,没有调用 malloc 或 free。zeros 缓冲区在堆栈上,while 循环不会写入超出缓冲区的末尾。问题出在代码的其他部分。解决问题需要多长时间取决于从多少地方调用 malloc/free/strdup 等。

于 2012-09-25T05:32:49.370 回答