Question

I am running a process, which is creating a file and using that file. After the end of that process, i am deleting that file.

If some exception arises in between then how to know that the file is released by the process or not and how to delete it if it is locked by the process.

Thanks for any help :)

Was it helpful?

Solution

On Windows, when the process ends, the OS returns all resources owned by that process automatically. End of story.

For example, say you did "CreateFileMapping()" on each of two processes to share a memory segment. When the first process exits, Windows decrements the usage counter, and only the surviving process can still use the object. When the second process does a "CloseHandle()" (or itself exits), the counter is decremented to zero, and the object is completely freed.

In other words, Windows will reclaim the resource when it's no longer used, whether the processes clean up gracefully after themselves or not.

And no, even if you open a file for exclusive access, the file itself won't be "locked" once the file handle is closed (and Windows WILL close it automatically when the program terminates).

OTHER TIPS

When your program ends - no matter by what means - the file, if it still exists, will no longer be locked by the process... because the process does not exist.

But I have to wonder; if the file is just being deleted anyway at the end, do you really need to create a file in the first place?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top