I saw there is lots of questions related to this, but I did not find one that is similar to mine. I am running a model written in mixed C and Fortran on a LSF platform. The wired thing is that my model was running fine until last week it starts to throw out this error. What's even wired is that the error does not occur every time: some times, the model can run (no error), sometimes the job will abort when trying to read in the input files. The error points to the code where I have never modified So far I have tried:
1) recompile the source code and use the newly-created executable;
2) Copy the executable from another directory that is running fine;
3) delete the whole directory and create a new directory and repeat the above two;
4)start from a fresh login
5)run only 1 job each time to rule out the possibility that influence from other jobs running on the same node
6)change the job name
7)change the running length (model year)
And the error still occur 90% of the times. The error points to inpakC.c file (I attached part of the file bellow) 'free(line)' part. I do not see anything wrong with this, since it is a prewritten code. Any help or advice will be greatly appreciated!
#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;
}