1

I have an application suite that I maintain for Windows platforms. I recently added some code to a shared library to remove a directory after the app is done with it. In one app, the deletion is successful; in the other, I receive a message telling me the file is in use by another process.

After downloading Process Explorer, I learned what I had already expected, that the process holding the folder is the one trying to delete it.

When I google for an answer, all I see is, "You need to download XYZ to find out what process is holding the file, then close that process," where "XYZ" is Unlocker, Process Explorer, etc. I know the process that is holding the file, but if I terminate it, how can it delete the folder?

Does anyone have any idea about how to locate the code that is holding the folder open? Of the tools that are available for finding which processes are using which files, can any be used to find where in the process the folder is open?

genpfault
  • 51,148
  • 11
  • 85
  • 139
OffByOne
  • 27
  • 1
  • 1
  • 6
  • If you've opened a file in your program, you must close it before deleting it. If you want more precise help, you should post some code – K Mehta Apr 11 '12 at 22:19
  • Perhaps the code that write in that directory? What do you think of posting that code? – Steve Apr 11 '12 at 22:19
  • I'd start by checking my program stack for any `fstream` objects, and whether they're closed. – suszterpatt Apr 11 '12 at 22:21
  • 3
    You can get this error if your app makes the folder the current folder (using `chdir` or equivalent), so make sure you've set the current folder somewhere else before trying to remove it. Checking before trying to remove the folder should be only a couple of lines of code. Other than that, trying to find software or a tool when you have the application source is just foolish, IMO. Search the source for places you're opening files or trying to delete files or folders; you'll find them with a simple `grep` much faster than you can find a tool and learn how to use it. – Ken White Apr 11 '12 at 22:42

1 Answers1

1

There is no concept of a "location in the process" where a file is open. E.g. a very common cause of unintentional open files are leaked handles. That means the file is open precisely because there is no location for the file handle in the process anymore.

MSalters
  • 173,980
  • 10
  • 155
  • 350