0

I read a lot about fclose and I have 2 questions about it:

1) my program crashes after a call to fclose(pFile), even though I checked that Pfile isn't NULL, and change it to NULL right after fclose. (see code below)

I have to mention that the crash doesn't happen any time the program run, but only at specific circumstances which I still don't know exactly what are they.

after the crash , I get an error - double free or corruption (!prev). so I guess it means that somehow fclose is being called twice on the same file descriptor, what I don't understand is how could this possibly happen?

    if (NULL != pFile)
    {
        fclose(pFile);
        pFile=NULL;
    }

    pFile = fopen (fullFilePath.c_str(), "r");

2) I know that calling fclose twice on the same file descriptor usually leads to crash, but I couldn't find an answer in the case that I have 2 file descriptors to the same file (lets say that there are 2 threads of the same process - each holds one file descriptor) . in this case, if both threads calling to fclose on the same file but through two different file descriptors, is that ok? it won't cause a crash?

almog
  • 51
  • 9
  • That means one thing, and one thing only. Your pfiles is not NULL, but already closed handle (or a handle which was never initialized). Closing two different descriptors opened for the same file is OK. – SergeyA Dec 02 '15 at 14:46
  • yes, I understood that, but the only place in my code that change pFile is this line of fopen that assign a new file descriptor to pFile. so I guess the only remain option is that somehow somewhere I have an memory Overriding that change pFile value to an invalid file descriptor...? – almog Dec 03 '15 at 09:13

0 Answers0