Release all open files and directories when strongly deleted in C

I want to release all open files and directories that my program opened during its execution. I want to do this because I have a very large program and it opens up a lot of files and directories that I cannot track. Is there any way to do this? means that I want to get a list of all open files and directories and close them when I exit.

I know the registration of exit handlers using the atexit () function. Is there anything you can do with it?

Edit: I have cygwin on windows. I want to do this because my software resources are not automatically released. I have a directory that is created and then opened with opendir (). After completing my program, when I try to delete this directory, it says: "can not delete used by another program." But when I finish explorer.exe and restart again, then only I can delete this directory.

The problem is that this happens unevenly. I can delete some directories and not delete some.

+4
source share
3 answers

If release means close, this will happen anyway. The C runtime and operating system will take care of this for you. At the end of the process, all resources that have been opened will be closed. It would be a very rare (and poor quality) environment that did not.

+4
source

Also check _ fcloseall ()

+1
source

man signal should give you some tips. This captures most termination signals, not just normal ones.

+1
source

Source: https://habr.com/ru/post/1301303/


All Articles