tags:

views:

124

answers:

3

I want to release all open files and directories forecefully my program has opened during its execution. I want to do this because I have a very big program and it opens many files and directories which I am not able to keep track. Is there any way to do it? means I want to retrieve a list of all open files and directories and close them at exit.

I know registration of exit handlers using atexit() function. Can something be done with it?

Edit: I have cygwin on windows. I want to do the above thing because my program's resources are not being automatically released. I have a directory which is created and then opened using opendir(). After my program finishes, when I try to delete that directory, it says "cant delete, being used by another program". But when I terminate explorer.exe and again restart, then only I am able to delete that directory.

The problem is it is happening unevenly. I am able to delete some directories and not able to delete some.

+4  A: 

If by "release" you mean "close", that will happen anyway. The C runtime and the operating system will take care of that for you. On process termination all resources the process had open will be closed off. It would be a very rare (and poor quality) environment that didn't do that.

Andrew
It is not doing in windows. I have a directory which is created and then opened using opendir(). After my program finishes, when I try to delete that directory, it says "cant delete, being used by another program". But when I terminate explorer.exe and again restart, then only I am able to delete that directory.
avd
But that's explorer holding the directory open -- not the operating system.
Andrew
But how, I dont have any window open of that directory and it is not open even at command prompt. How another program is using it? It happens only after running my program so definitely it has something to do with my program
avd
@lex - Are you sure your process is ending? Check task manager processes by name
Carlos Gutiérrez
Ya sure, it is ending properly. The problem is it is happening unevenly. I am able to delete some directories and not able to delte some.
avd
+1  A: 

Also check _fcloseall()

Carlos Gutiérrez
Is this in standard library? Is there anything similar for closing directories?
avd
**note:** `fcloseall(3)` exists in Linux as a GNU extension
jschmier
@jschmier - it's available in cygwin
Carlos Gutiérrez
I am opening directories using opendir(). Are u sure I dont to close them?
avd
since `fcloseall` doesn't take any arguments, you may be able to register it with `atexit` and it should be called prior to the OS closing any streams and/or files
jschmier
+1  A: 

man signal should give you some hints. This catches most of termination signals, not only normal ones.

mouviciel