c语言 - 如何处理文件处理功能" *** glibc detected *** ./a: double free or corruption (top): "错误?



我没有使用动态内存分配,没有在任何地方释放内存。它甚至给出了两个自由误差。有谁能帮帮我吗?

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/inotify.h>
#include <limits.h>
#define MAX_EVENTS 1024 /*Max. number of events to process at one go*/
#define LEN_NAME 16 /*Assuming that the length of the filename won't exceed 16 bytes*/
#define EVENT_SIZE  ( sizeof (struct inotify_event) ) /*size of one event*/
#define BUF_LEN     ( MAX_EVENTS * ( EVENT_SIZE + LEN_NAME )) /*buffer to store the data of events*/
#define SIZE 20000
int main( int argc, char **argv ) 
{
    int length, i = 0, wd;
    int fd;
    char buffer[BUF_LEN];
    time_t timer;
    char buffer2[25];
    char *s=NULL;
    struct tm* tm_info;
    FILE *fk;
    //char buffer[EVENT_BUF_LEN];
    char str1[] = "File has been updated at time :";
    time(&timer);
    tm_info = localtime(&timer);
    strftime(buffer2, 25, "%Y:%m:%d %H:%M:%S", tm_info);
    fk = fopen( "/home/technoworld/Desktop/file.txt" , "w" );
    /* Initialize Inotify*/
    fd = inotify_init();
    if ( fd < 0 ) {
        perror( "Couldn't initialize inotify");
    }
    /* add watch to starting directory */
    wd = inotify_add_watch(fd, argv[1], IN_MODIFY ); 
    if (wd == -1)
    {
        printf("Couldn't add listen to %sn",argv[1]);
    }
    else
    {
        printf("Listening: %sn",argv[1]);
    }
    /* do it forever*/
    while(1)
    {
        i = 0;
        length = read( fd, buffer, BUF_LEN );  
        if ( length < 0 ) {
            perror( "read" );
        }  
        while ( i < length ) {
            struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
            if ( event->len ) {
                if ( event->mask & IN_MODIFY) {
                    if (event->mask & IN_ISDIR)
                        printf( "The directory %s was modified.n", event->name );       
                    else
                    {
                        printf( "The file %s was modified at %s n", event->name, buffer2 ); 
                        fprintf(fk,"%s %s %c",str1, buffer2, 'n');
                    }
                }
                i += EVENT_SIZE + event->len;
            }
        }
        fcloseall(fk);
    }
    /* Clean up*/
    inotify_rm_watch( fd, wd );
    close( fd );
    return 0;
}

我正在尝试收听文件中的更改,并将更改写入带有时间戳的日志文件。

错误:

Listening: /home/technoworld/Desktop/tmp/
The file .goutputstream-L3JTXW was modified at 2013:05:24 22:06:21 
The file .goutputstream-IRR4XW was modified at 2013:05:24 22:06:21 
*** glibc detected *** ./a: double free or corruption (fasttop): 0x09438008 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb7624ee2]
./a[0x80488bb]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb75c84d3]
./a[0x80485c1]
======= Memory map: ========
08048000-08049000 r-xp 00000000 07:00 558766     /home/technoworld/Desktop/a
08049000-0804a000 r--p 00000000 07:00 558766     /home/technoworld/Desktop/a
0804a000-0804b000 rw-p 00001000 07:00 558766     /home/technoworld/Desktop/a
09438000-09459000 rw-p 00000000 00:00 0          [heap]
b7578000-b7594000 r-xp 00000000 07:00 394123     /lib/i386-linux-gnu/libgcc_s.so.1
b7594000-b7595000 r--p 0001b000 07:00 394123     /lib/i386-linux-gnu/libgcc_s.so.1
b7595000-b7596000 rw-p 0001c000 07:00 394123     /lib/i386-linux-gnu/libgcc_s.so.1
b75ae000-b75af000 rw-p 00000000 00:00 0 
b75af000-b7752000 r-xp 00000000 07:00 394098     /lib/i386-linux-gnu/libc-2.15.so
b7752000-b7753000 ---p 001a3000 07:00 394098     /lib/i386-linux-gnu/libc-2.15.so
b7753000-b7755000 r--p 001a3000 07:00 394098     /lib/i386-linux-gnu/libc-2.15.so
b7755000-b7756000 rw-p 001a5000 07:00 394098     /lib/i386-linux-gnu/libc-2.15.so
b7756000-b7759000 rw-p 00000000 00:00 0 
b776e000-b7773000 rw-p 00000000 00:00 0 
b7773000-b7774000 r-xp 00000000 00:00 0          [vdso]
b7774000-b7794000 r-xp 00000000 07:00 394076     /lib/i386-linux-gnu/ld-2.15.so
b7794000-b7795000 r--p 0001f000 07:00 394076     /lib/i386-linux-gnu/ld-2.15.so
b7795000-b7796000 rw-p 00020000 07:00 394076     /lib/i386-linux-gnu/ld-2.15.so
bfe5d000-bfe7e000 rw-p 00000000 00:00 0          [stack]
Aborted (core dumped)

fcloseall不接受任何参数。(这是怎么编译的?)

即使初始的fopen是成功的(因此fk不是NULL),在通过while (1)循环的第一次行程之后,您将调用fcloseall并因此关闭文件。因此,fprintffk被打印到一个关闭的文件。由于文件操作分配和使用内存,而关闭操作释放内存,因此您使用的是释放的内存,因此出现glibc错误。

问题就在那里,因为你在64位系统上工作,你不包括time.h和localtime(&timer)的返回类型被解释为32位整数而不是64位指针(未声明的类型默认为int)…您正在丢失隐式声明中的信息。

石蕊试验看看我是否正确:

在include下面声明以下内容:

#include <limits.h>
struct tm;
struct timep;
struct tm *localtime(const time_t *timep);

SIGSEG将消失(尽管您的代码仍然有bug)

正确的做法是#include <time.h>

我在while(1)完成后加入了fclose(fk)。它不会给出任何错误,并且在终端上运行良好。但是在这个例子中,它没有写入file.txt。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/inotify.h>
#include <limits.h>
#include <time.h>
#define MAX_EVENTS 1024 /*Max. number of events to process at one go*/
#define LEN_NAME 16 /*Assuming that the length of the filename won't exceed 16 bytes*/
#define EVENT_SIZE  ( sizeof (struct inotify_event) ) /*size of one event*/
#define BUF_LEN     ( MAX_EVENTS * ( EVENT_SIZE + LEN_NAME )) /*buffer to store the data of events*/
#define SIZE 20000
int main( int argc, char **argv ) 
{
  int length, i = 0, wd;
  int fd;
  char buffer[BUF_LEN];

    time_t timer;
    char buffer2[25];
    char *s=NULL;
    struct tm* tm_info;
  FILE *fk;
  //char buffer[EVENT_BUF_LEN];
  char str1[] = "File has been updated at time :";
    time(&timer);
    tm_info = localtime(&timer);
    strftime(buffer2, 25, "%Y:%m:%d %H:%M:%S", tm_info);
   fk = fopen( "/home/technoworld/Desktop/file.txt" , "w" );

  /* Initialize Inotify*/
  fd = inotify_init();
  if ( fd < 0 ) {
    perror( "Couldn't initialize inotify");
  }
  /* add watch to starting directory */
  wd = inotify_add_watch(fd, argv[1], IN_MODIFY ); 
  if (wd == -1)
    {
      printf("Couldn't add listen to %sn",argv[1]);
    }
  else
    {
      printf("Listening: %sn",argv[1]);
    }
  /* do it forever*/
  while(1)
    {
      i = 0;
      length = read( fd, buffer, BUF_LEN );  
      if ( length < 0 ) {
        perror( "read" );
      }  
      while ( i < length ) {
        struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
        if ( event->len ) {
          if ( event->mask & IN_MODIFY) {
            if (event->mask & IN_ISDIR)
              printf( "The directory %s was modified.n", event->name );       
            else
        {
                    printf( "The file %s was modified at %s n", event->name, buffer2 ); 
                    fprintf(fk,"%s %s %c",str1, buffer2, 'n');
        }
          }
            i += EVENT_SIZE + event->len;
        }
      }
    }
fclose(fk);
  /* Clean up*/
  inotify_rm_watch( fd, wd );
  close( fd );
  return 0;
}

相关内容