即使执行了free,程序也会崩溃

时间:2012-06-19 10:00:14

标签: c file ubuntu error-handling io

鉴于此代码是更大代码的一部分,但这是责任。

int main()我这样做:

int main(int argc, char* argv[]) {

// code 

// code 

path2 = (char*)malloc(strlen(currentDir)*sizeof(char));

traverseDirectories(path2 , filename);

free(path2);
}

错误:

a@ubuntu:~/Desktop$ ./exer4 fsdfsdfdafsd/ exer4
Could not open: No such file or directory
*** glibc detected *** ./exer4: free(): invalid next size (normal): 0x08b54038 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x6ebc2)[0x93ebc2]
/lib/i386-linux-gnu/libc.so.6(+0x6f862)[0x93f862]
/lib/i386-linux-gnu/libc.so.6(cfree+0x6d)[0x94294d]
/lib/i386-linux-gnu/libc.so.6(fclose+0x154)[0x92e0b4]
/lib/i386-linux-gnu/libc.so.6(perror+0xc5)[0x920a45]
./exer4[0x8048f52]
./exer4[0x8048b34]
./exer4[0x8048a99]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x8e9113]
./exer4[0x80487c1]
======= Memory map: ========
00398000-003b4000 r-xp 00000000 08:01 394171     /lib/i386-linux-gnu/libgcc_s.so.1
003b4000-003b5000 r--p 0001b000 08:01 394171     /lib/i386-linux-gnu/libgcc_s.so.1
003b5000-003b6000 rw-p 0001c000 08:01 394171     /lib/i386-linux-gnu/libgcc_s.so.1
008d0000-00a46000 r-xp 00000000 08:01 394150     /lib/i386-linux-gnu/libc-2.13.so
00a46000-00a48000 r--p 00176000 08:01 394150     /lib/i386-linux-gnu/libc-2.13.so
00a48000-00a49000 rw-p 00178000 08:01 394150     /lib/i386-linux-gnu/libc-2.13.so
00a49000-00a4c000 rw-p 00000000 00:00 0 
00b5a000-00b5b000 r-xp 00000000 00:00 0          [vdso]
00c4b000-00c69000 r-xp 00000000 08:01 394137     /lib/i386-linux-gnu/ld-2.13.so
00c69000-00c6a000 r--p 0001d000 08:01 394137     /lib/i386-linux-gnu/ld-2.13.so
00c6a000-00c6b000 rw-p 0001e000 08:01 394137     /lib/i386-linux-gnu/ld-2.13.so
08048000-0804a000 r-xp 00000000 08:01 947365     /home/a/Desktop/exer4
0804a000-0804b000 r--p 00001000 08:01 947365     /home/a/Desktop/exer4
0804b000-0804c000 rw-p 00002000 08:01 947365     /home/a/Desktop/exer4
08b54000-08b75000 rw-p 00000000 00:00 0          [heap]
b7600000-b7621000 rw-p 00000000 00:00 0 
b7621000-b7700000 ---p 00000000 00:00 0 
b775f000-b7760000 rw-p 00000000 00:00 0 
b776f000-b7771000 rw-p 00000000 00:00 0 
bf87d000-bf89e000 rw-p 00000000 00:00 0          [stack]
Aborted

以下是方法:

int traverseDirectories(char * path , const char * filename)
{

    // printf("\nCurrent path is :%s\n " , path);
    struct dirent *direntp = NULL;
    DIR *dirp = NULL;
    size_t path_len;
    char full_name[BUFFER];
    strcpy(full_name, path);

    displayAllFiles(full_name , filename);  // display the file details to STDOUT

// code 
// code 

}


void displayAllFiles(char * directory , char * filename)
{
    struct stat st;
    DIR *dir;
    struct dirent *ent;

    char fullPathDirectory[BUFFER];
    char fileDetails[BUFFER];

    memset(fullPathDirectory, '\0', BUFFER);   // reset the array of the input
    memset(fileDetails, '\0', BUFFER);   // reset the array of the input

    dir = opendir (directory);

    if (dir != NULL)
    {

         // print all the files and directories within directory
         while ((ent = readdir (dir)) != NULL)
         {
             if (strcmp(ent->d_name , filename) == 0)
             {
                  // printf("\nFile found!\n");

                  strcpy(fullPathDirectory , directory);
                  strcat(fullPathDirectory , "/");
                  strcat(fullPathDirectory , filename);

                  if (lstat(fullPathDirectory, &st) < 0)
                  {
                      perror(ent->d_name);
                      putchar('\n');
                      continue;
                  }

                  displayProperties(&st , fileDetails);

                  printf("\n%s" , fileDetails); // print file details
                  printf("%s\n" , fullPathDirectory); // print file path
             }

         }
         closedir (dir);
    }

    else
    {
     // could not open directory
     perror ("Could not open");
     free(directory);
     exit(EXIT_FAILURE);
    }

}

free方法directory中的displayAllFiles,但即使我在执行时收到此消息:

./exer4 fsdfsdfdafsd/ exer4

程序找不到文件夹fsdfsdfdafsd,这没关系,但为什么要打印所有的回溯?我想要它做的就是打印no such file or directory

有什么想法吗?

谢谢

5 个答案:

答案 0 :(得分:4)

一些快速观察:

  1. Don't cast the return of malloc()
  2. sizeof (char)是1。
  3. 字符串需要'\0'终止字符的空间。

答案 1 :(得分:1)

free(directory)来自displayAllFiles()中的本地变量时,您会在directory中致电traverseDirectories()

通常,你不应该在那里调用exit(),而只是返回错误的指示符,调用者可以(并且应该)处理错误。

答案 2 :(得分:1)

这是你的一些代码。

char full_name[BUFFER];    
displayAllFiles(full_name , filename); 
.....
void displayAllFiles(char * directory , char * filename)
....
free(directory);

参数directory未由malloc分配,free未由malloc分配的地址将导致未定义的行为。

答案 3 :(得分:1)

我写了一个测试代码来模拟你的问题。

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 void problem(char * addr)
  5 {
  6     free(addr);
  7 }
  8 
  9 int func(char * buf)
 10 {
 11     char name[10];
 12 
 13 
 14 
 15     problem(name);
 16 
 17     return 0;
 18 }
 19 
 20 int main(void)
 21 {
 22     char *string;
 23 
 24     string = NULL;
 25     string = malloc(10);
 26     if(string == NULL)
 27         return -1;
 28 
 29     func(string);
 30     free(string);
 31     return 0;
 32 }
 33 

运行valgrind给出消息。

==17451== Invalid free() / delete / delete[] / realloc()
==17451==    at 0x59CFC: free (in /usr/local/lib/valgrind/vgpreload_memcheck-x86-freebsd.so)
==17451==    by 0x8048490: problem (main.c:6)
==17451==    by 0x80484B0: func (main.c:15)
==17451==    by 0x8048500: main (main.c:29)
==17451==  Address 0xbebfec2e is on thread 1's stack

您无法释放未由malloc分配的地址,这会导致问题。

答案 4 :(得分:0)

问题在于:

a@ubuntu:~/Desktop$ ./exer4 fsdfsdfdafsd/ exer4
Could not open: No such file or directory
*** glibc detected *** ./exer4: free(): invalid next size (normal): 0x08b54038 ***

事情是,当我分配这个时:

path2 = (char*)malloc(strlen(currentDir)*sizeof(char));

稍后更改path2指向的字符串的大小,然后尝试在main中重新使用 一个长度为strlen(currentDir)的字符串,然后我明白了:

 invalid next size (normal): 0x08b54038 ***

这意味着我尝试释放一些其他大小的内存,当然这是不正确的。 这就是我这样做的原因:path2 = malloc(BUFFER*sizeof(char));,一切顺利。

谢谢大家!