2016-07-07 57 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試圖發現這種問題的工具下訪問

  • 運行。

  • 研究的內存調試程序下的內容,並試圖推斷出了什麼問題。

相關問題