先前关闭的文件句柄上的fclose()发生双重错误 [英] Double free error on fclose() on previously closed file handle
问题描述
我有一个包含文件句柄 fHandle
的类,它指向一个打开的文件。现在,它可以被多个例程中的任何一个关闭,并且基于程序的动态运行。
为了确保文件确实关闭,我把简单的代码片断放在我的析构函数中: if(fHandle!= NULL)fclose(fHandle);
事实证明,如果其中一个例程先前已经关闭了这个文件,那么运行析构函数代码片断会导致双重释放操作,并且我得到 ** glib被检测到**
错误信息。 / p>
如何确保我不关闭先前已关闭的文件句柄(除了放置 NULL
检查)?
你说你已经试过了:
if(fHandle!= NULL)fclose(fHandle);
为了避免出现双 fclose()
只要将 fHandle
设置为 NULL
您可以调用 fclose
在那个句柄上,所以上面的代码在析构函数中不会通过条件测试....
I have a class that contains file handle fHandle
that points to an open file. Now, it can be closed by any one of the multiple routines and that is based on dynamic run of the program.
To be sure that a file is indeed closed, I put simple snippet in my destructor: if(fHandle!=NULL) fclose(fHandle);
. Turns out, if one of the routines had previously closed this file, then the running the destructor snippet causes double free operation and I get **glib detected**
error message.
How do I make sure that I don't close the file handle that has previously been closed (apart from putting NULL
check)?
You say you've tried:
if(fHandle!=NULL) fclose(fHandle);
To avoid a double fclose()
, just set fHandle
to NULL
whereever else you may call fclose
on that handle, so the above code in the destructor won't pass the conditional test....
这篇关于先前关闭的文件句柄上的fclose()发生双重错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!