0

我是在 linux 环境中编译的新手。无论如何,我有一个 C 源代码,它假设读取一系列 .pgm 图像文件,压缩它们并将压缩文件写入新的 .pgm 文件。但是,当我编译时,我收到以下错误消息

*** glibc detected *** ./a.out: free(): invalid pointer: 0x00007fefc6176010 ***  
======= Backtrace: =========  
/lib/x86_64-linux-gnu/libc.so.6(+0x7a6e6)[0x7fefda73b6e6]  
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x6c)[0x7fefda73f9cc]  
./a.out[0x408491]  
./a.out[0x408565]  
./a.out[0x401048]  
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7fefda6e230d]  
./a.out[0x400a59]  
======= Memory map: ========    
00400000-0040b000 r-xp 00000000 07:00 299109                             /home/skyjuice/PROJECT3/a.out  
0060a000-0060b000 r--p 0000a000 07:00 299109                             /home/skyjuice/PROJECT3/a.out  
0060b000-0060c000 rw-p 0000b000 07:00 299109                             /home/skyjuice/PROJECT3/a.out  
0060c000-059f8000 rw-p 00000000 00:00 0   
05ebb000-05ee3000 rw-p 00000000 00:00 0                                  [heap]  
7fefc0000000-7fefc0021000 rw-p 00000000 00:00 0   
7fefc0021000-7fefc4000000 ---p 00000000 00:00 0   
7fefc59d3000-7fefc59d4000 rw-p 00000000 00:00 0   
7fefc5f60000-7fefc5f75000 r-xp 00000000 07:00 2117                       /lib/x86_64-linux-gnu/libgcc_s.so.1
7fefc5f75000-7fefc6174000 ---p 00015000 07:00 2117                       /lib/x86_64-linux-gnu/libgcc_s.so.1
7fefc6174000-7fefc6175000 r--p 00014000 07:00 2117                       /lib/x86_64-linux-gnu/libgcc_s.so.1
7fefc6175000-7fefc6176000 rw-p 00015000 07:00 2117                       /lib/x86_64-linux-gnu/libgcc_s.so.1  
7fefc6176000-7fefda6c1000 rw-p 00000000 00:00 0   
7fefda6c1000-7fefda858000 r-xp 00000000 07:00 904195                     /lib/x86_64-linux-gnu/libc-2.13.so  
7fefda858000-7fefdaa57000 ---p 00197000 07:00 904195                     /lib/x86_64-linux-gnu/libc-2.13.so  
7fefdaa57000-7fefdaa5b000 r--p 00196000 07:00 904195                     /lib/x86_64-linux-gnu/libc-2.13.so  
7fefdaa5b000-7fefdaa5c000 rw-p 0019a000 07:00 904195                     /lib/x86_64-linux-gnu/libc-2.13.so  
7fefdaa5c000-7fefdaa62000 rw-p 00000000 00:00 0   
7fefdaa62000-7fefdaae5000 r-xp 00000000 07:00 904199                     /lib/x86_64-linux-gnu/libm-2.13.so  
7fefdaae5000-7fefdace4000 ---p 00083000 07:00 904199                     /lib/x86_64-linux-gnu/libm-2.13.so  
7fefdace4000-7fefdace5000 r--p 00082000 07:00 904199                     /lib/x86_64-linux-gnu/libm-2.13.so  
7fefdace5000-7fefdace6000 rw-p 00083000 07:00 904199                     /lib/x86_64-linux-gnu/libm-2.13.so  
7fefdace6000-7fefdad07000 r-xp 00000000 07:00 644102                     /lib/x86_64-linux-gnu/ld-2.13.so  
7fefdaeeb000-7fefdaeee000 rw-p 00000000 00:00 0   
7fefdaf04000-7fefdaf06000 rw-p 00000000 00:00 0   
7fefdaf06000-7fefdaf07000 r--p 00020000 07:00 644102                     /lib/x86_64-linux-gnu/ld-2.13.so  
7fefdaf07000-7fefdaf09000 rw-p 00021000 07:00 644102                     /lib/x86_64-linux-gnu/ld-2.13.so  
7fff1b020000-7fff1b0ca000 rw-p 00000000 00:00 0                          [stack]  
7fff1b148000-7fff1b149000 r-xp 00000000 00:00 0                          [vdso]  
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]  
Aborted

下面是源代码的片段

#define START_FRAME 141 #define END_FRAME 143 #define SKIP_FRAME 1 #define INPUT_STRING "/home/image%03d.pgm"

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

  unsigned char *frm1, *frm2, *predict, *predictDb, *frm1E;    
  unsigned char *rcnDiff, *diff, *rcnFrm1, *rcnFrm2, *wave;  
  int           *xVct, *yVct, *pxE, *pyE, *code;  
  int   nx  = 1920, ny = 1080, nxy;  
  int   bSize  = 16, nbx, nby, nbxy;
  int   frameNo = START_FRAME;  
  int   nxy  = nx * ny;

  frm1       = (unsigned char *) malloc(sizeof(unsigned char) * nxy);  
  frm2       = (unsigned char *) malloc(sizeof(unsigned char) * nxy);  
  rcnFrm1    = (unsigned char *) malloc(sizeof(unsigned char) * nxy);  
  rcnFrm2    = (unsigned char *) malloc(sizeof(unsigned char) * nxy);  
  predict    = (unsigned char *) malloc(sizeof(unsigned char) * nxy);  
  predictDb  = (unsigned char *) malloc(sizeof(unsigned char) * nxy);  
  diff       = (unsigned char *) malloc(sizeof(unsigned char) * nxy);  
  rcnDiff    = (unsigned char *) malloc(sizeof(unsigned char) * nxy);  
  wave       = (unsigned char *) malloc(sizeof(unsigned char) * nxy);    
  xVct       = (int *) malloc(sizeof(int) * nbxy);  
  yVct       = (int *) malloc(sizeof(int) * nbxy);  

  code       = (int *) malloc(sizeof(int) * tgtBits1);    
  frm1d      = (double *) malloc(sizeof(double) * nxy);  
  frm2d      = (double *) malloc(sizeof(double) * nxy);  
  predictd   = (double *) malloc(sizeof(double) * nxy);  
  predictDbd = (double *) malloc(sizeof(double) * nxy);  
  rcnFrm1d   = (double *) malloc(sizeof(double) * nxy);  
  rcnFrm2d   = (double *) malloc(sizeof(double) * nxy);  
  wavFrm1    = (double *) malloc(sizeof(double) * nxy);  
  wavDiff    = (double *) malloc(sizeof(double) * nxy);  
  wavRcnFrm1 = (double *) malloc(sizeof(double) * nxy);  
  wavRcnDiff = (double *) malloc(sizeof(double) * nxy);  
  diffd      = (double *) malloc(sizeof(double) * nxy);  
  rcnDiffd   = (double *) malloc(sizeof(double) * nxy);  

  frm1Ed     = (double *) malloc(sizeof(double) * nxE * nyE);  
  frm1E      = (unsigned char *) malloc(sizeof(unsigned char) * nxE * nyE);

  //read file from PGM and store into frm1  
  frameNo = START_FRAME;  
  sprintf(fileName, INPUT_STRING, frameNo);  
  readPgm(frm1, fileName, nx, ny);  

  //after some operations

  //write into a new PGM file  
   sprintf(fileName, "predictDb%03d.pgm", frameNo);  
   writePgm(predictDb, fileName, nx, ny);  

  return 0; //end of main function   
}  

//read original frame  
void readPgm(unsigned char *frame, char * fileName, int nx, int ny) {  
  int i, j;  
  FILE *file;  
  char str[128];  

  file = fopen(fileName,"r");  
  if (file == 0) {  
    printf("Open error in File read \n");  
      }  
  fgets(str, 100, file);  
  for(j = 0; j < ny; j++) {  
      for(i = 0; i < nx; i++) {  
    fscanf(file,"%d", (int *)&frame[i + nx * j]);  
      }  
  }  
  fclose(file);  
}  


//create frame  
void writePgm(unsigned char *frame, char *fileName, int nx, int ny){  
  int i, j;  
  FILE *file;  

  file = fopen(fileName, "w");  
  fprintf(file, "P2 %d %d 255\n", nx, ny);  
  for(j = 0; j < ny; j++) {  
    for(i = 0; i < nx; i++) {  
      fprintf(file,"%d ", frame[i + nx * j]);  
    }  
    fprintf(file, "\n");  
  }  
  fclose(file);  
}  

任何人都可以帮助确定问题所在吗?谢谢

4

3 回答 3

1

您永远不会初始化nxy您在调用中使用的大多数其他变量malloc()。然后这些变量可以包含随机值,您将不会分配正确的内存量。

于 2012-04-16T03:58:23.150 回答
1

这段代码

int   /* ... */, nxy;   
frm1  = (unsigned char *) malloc(sizeof(unsigned char) * nxy);   

意味着您根据未初始化的值 ( nxy) 分配内存,它可以是任何东西(无论内存中nxy分配的垃圾是什么。

然后你将这个未知长度的缓冲区传递给

readPgm(frm1, fileName, nx, ny);

并尝试访问frame[i + nx * j]

如果nxy小于nx*ny,则通过写入分配的空间来破坏内存。

要修复,请确保正确初始化所有变量。例如:

int nxy = nx*ny;

另外,请参阅this SO question on why you should not cast the return value of malloc-- void *(的返回类型malloc)将隐式转换为正确的类型

于 2012-04-16T03:59:28.107 回答
0

这似乎不是特定于 Linux 的——C 代码应该可以在任何环境中工作,甚至是 Windows。

gcc -g使用(或什至)编译gcc -ggdb以获取该错误回溯中的行号(并使用 gdb 进行更轻松的调试)。

我看不到你在哪里调用 free() 来释放你使用 malloc() 分配的内存。如果不这样做,则在进程退出时释放内存。我怀疑你有一个错误的溢出到包含指针的内存中,并用垃圾覆盖指针中的地址。然后,当您尝试释放从该“地址”开始的一块内存时,操作将失败。使用调试信息进行编译,在 gdb ( gdb --args ./myprogram arg1 arg2, start, next, step, continue) 中运行程序并查看问题出现在哪里。为了帮助调试内存问题(如缓冲区外的写入),请使用 valgrind。

于 2012-04-16T03:54:23.527 回答