我看到有很多与此相关的问题,但我找不到与我相似的问题。我在LSF平台上运行用混合C和Fortran编写的模型。有线的事情是我的模型运行良好,直到上周它开始抛出这个错误。甚至有线的是每次都不会发生错误:有时,模型可以运行(没有错误),有时候在尝试读取输入文件时作业将中止。错误指向我从未修改过的代码 到目前为止,我已经尝试过:
1)重新编译源代码并使用新创建的可执行文件;
2)从另一个运行正常的目录中复制可执行文件;
3)删除整个目录并创建一个新目录并重复上述两个;
4)从新登录开始
5)每次只运行一个作业,以排除在同一节点上运行的其他作业的影响的可能性
6)更改工作名称
7)改变跑步长度(型号年份)
错误仍然发生在90%的时间。错误指向inpakC.c文件(我附加了文件的一部分)'free(line)'部分。我没有看到任何错误,因为它是一个预先编写的代码。任何帮助或建议将不胜感激!
#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 :(得分:0)
错误意味着某些东西试图在一个无效的免费指针上调用free()。要么它不是来自malloc(),要么它已经被释放了。这通常表示内存损坏。程序中的某些东西会覆盖它不应该存在的内存。可能是因为它写入了一个索引无效的数组。像你所描述的那样以不可预测的方式再现这种问题并不罕见。
这种问题很难追查。一些方法包括:
在数组访问旁边放置检查断言的边界
在valgrind或address sanatizer等工具下运行,试图检测此类问题。
在调试器下研究内存的内容,并尝试推断出错了。