2016-07-07 92 views
-2

我看到有很多与此有关的问题,但我没有找到一个类似于我的。我在LSF平台上运行混合C和Fortran编写的模型。有线的事情是,我的模型运行良好,直到上周它开始抛出这个错误。甚至有线连接错误不会每次都发生:有时,模型可以运行(无错误),有时在尝试读取输入文件时作业会中止。错误指向我从未修改过的代码 到目前为止,我尝试过:非常奇怪的错误*** glibc检测**免费()无效指针

1)重新编译源代码并使用新创建的可执行文件;

2)从另一个运行正常的目录复制可执行文件;

3)删除整个目录并创建一个新目录并重复上述两个步骤;

4)从一个全新的登录

5开始)运行每次排除同一节点

6)改变作业名称上运行的其他工作影响的可能性只有1项作业

7)更改运行长度(型号年)

而且错误仍然发生在90%的时间。错误指向inpakC.c文件(我附上了文件的一部分)“免费(线)”部分。我没有看到任何错误,因为它是一个预先编写的代码。任何帮助或建议将不胜感激!

enter image description here

enter image description here

#ifdef MPI 
int ipck_LoadF(char *filename, MPI_Comm comm) 
#else 
int ipck_LoadF(char *filename) 
#endif 
{ 
    /*local variables */ 
    FILE *fileptr;    /*pointer to the file */ 
    int bsize;     /*buffer size (this was the default used in   

    int maxLsize;     /*max line size(this was the default used in 
    char *line;     /*the next line in the file */ 
    int n, m, clrt; 
    int my_address; 
    int c; 

    my_address =0; 
    #ifdef MPI 
    MPI_Comm_rank(comm, &my_address); 
    #endif 
if(my_address == 0){ 
    bsize = 0; 
    maxLsize = 0; 
    clrt = 1; /*current line running total set to zero*/ 

    /*open the file */ 
    /*if the file was not opened, exit and return 1 */ 
    if ((fileptr = fopen(filename, "r")) == NULL) 
    { 
return 1; 
    } 
    /*go through file and count the number of elements - used to know how much mem to allocate*/ 
    while ((c = fgetc(fileptr)) != EOF) 
    { 
    bsize++; 
    clrt++; 
    /*get length of longest line*/ 
    if (c == '\n')/*end of the line has been reached*/ 
     { 
     if (clrt > maxLsize)/*line contains the most char so far*/ 
     { 
     maxLsize = clrt; 
     clrt = 1; 
     } 
     else /*line has less char than the record so just reset the counter*/ 
     { 
     clrt = 1; 
     } 
     } 
     } 
    /*allocate mem for the buffer*/ 
    buffer = (char *) calloc(bsize, sizeof(char)); 
    /*postion pointer back to the begining*/ 
    rewind(fileptr); 

    /*read the contents of the file into the buffer variable */ 
    while (!feof(fileptr)) 
    { 
     /*allocate memory to hold the line to read into and the trimmed line */ 
     line = (char *) calloc(maxLsize, sizeof(char)); 

     /*get the next line */ 
     fgets(line, maxLsize, fileptr); 

     /*see if the next line is blank; if so skip the rest 
     and continue retrieving lines*/ 
     if(strcmp(line, "\n")==0) continue; 

     /*get the position of the comment character. 
     if one does not exist, it will return the length of the string*/               
     n=strcspn(line,"#"); 

     m=n-2; 
     while (*(line+m)==' ' || *(line+m)=='/' || *(line+m)=='\n'){ 
     n--; 
     m--; 
     } 

if (n > 0){ 
    /*cat n-1 chars to the buffer  */ 
    strncat(buffer,line,n-1); 
} 


/*put a padded space after the new line added to the buffer */ 
strcat(buffer, " "); 
/*clean up strings and flush */ 
free(line); 
fflush(fileptr); 
} 
/*close the file */ 
fclose(fileptr); 
    } 
     /*broadcast to all of the nodes*/ 
     #ifdef MPI 
     MPI_Bcast(&bsize,1,MPI_INT,0,comm); 
     if (my_address != 0) 
     buffer = (char *) calloc(bsize, sizeof(char)); 
      MPI_Bcast(buffer,bsize,MPI_CHAR,0,comm); 
     #endif 
     return 0; 
    } 
+0

使用[Valgrind的](http://valgrind.org)。如果您执行无效的内存访问,它会告诉你在哪里。 – dbush

+0

您可能需要先修改缩进和不正确的评论。 – EOF

+0

并且不要发布文字的图像! – Olaf

回答

0

的错误意味着什么试图调用free()上的指针是无效的免费打电话。它不是来自malloc(),或者它已经被释放。这通常表示内存损坏。你的程序中的某些东西正在覆盖它不应该存在的内存。可能是因为它正在写入一个索引无效的数组。像你所描述的那样,以这种不可预测的方式重现这类问题并不罕见。

这种问题很难追查到。一些方法包括:

  • 放边界检查断言旁边的阵列像valgrindaddress sanatizer试图发现这种问题的工具下访问

  • 运行。

  • 研究的内存调试程序下的内容,并试图推断出了什么问题。

相关问题