views:

252

answers:

4

I am writing a desktop application under Windows in C++ MFC.

The application creates a index file, and writes information to it again and again.

If the application crashes, the next time the application starts it will delete the crashed index file and create a new one. I think in certain cases, the index file will be locked. It will be a disaster if I can not delete the locked index file.

How can I guarantee that I can delete the file and create a new one? I can make sure no other application opens the index file. It cannot be deleted just because of an application crash.

Can any one help?

+7  A: 

If there's no process keeping the file open, there's no way it can remain locked. You might find that as long as your crashed process does actually die (rather than hanging) you have no problem.

If you really do need to be sure that you can delete the file from one process while another process has it open, you need both processes to open it with the FILE_SHARE_DELETE flag.

RichieHindle
Its not entirely true that if there isn't a process open then its not locked. In a few odd cases Windows fails to lose the lock. Its a very rare issue though but I have seen it happen a number of times, even once under Win 7. Never have worked out how to re-create it, though. It only seems to happen on crashes though.
Goz
When blue screen happens, this lock may happend
I would be very surprised if you find a bug where a lock survives a handle closing and process termination. If you do, it is a basic bug in the operating system. To make that assertion, you need to rule out a bug in your code. Could a handle have survived in another process? Did the process really die, or is a debugger keeping it alive? What does "crash" mean in this context? Given that you can't reproduce it, I am very sceptical of the assertion that it is an operating system bug.
janm
A: 

Most likely the answer is "you cannot delete a locked file". The OS won't let you.

Instead I would work around it with something like this.

  • Check for existing index files, delete them if no locks are present.
  • Create a new index file, if the first "name" is taken, check for index01 and so forth until you find one not in use, then use that as your indexfile for this execution of the program.
  • On normal exit, clear your index file.

Bonus credit: allow users to "recover" a crashed index file instead of deleting them automatically.

Grubsnik
+3  A: 

To unlock file you should close all handles associated with it. The best way to do it is to terminate crashed application that still running (and owns file handle).

To find crashed application you could use technique that described in this article. That is what Process Explorer does when you search handles of files with specified name.

Kirill V. Lyadvinsky
It is a bad idea to close handles in another process. The process that owns the handler doesn't know it has been closed, the value can get reused, and then all kinds of strange problems can occur.
janm
If you are going to do this, you should just terminate the process with the handle open. At least that way things stop rather than fail in unexpected ways.
janm
Yes. If "crashed" means "terminated", then there would be no need to close a handle in another process because the process is gone. If "crashed" means "not making forward process", then the correct thing to do is to terminate the process, and terminate its open handles with it. Closing a handle in another process without its knowledge is not the correct thing to do.
janm
Thanks. I suggest removing the suggestion that closing the handles is acceptable. It is almost never The Right Thing To Do.
janm
@janm, edited the answer. Thanks for remarks.
Kirill V. Lyadvinsky
A: 

Sometimes it's possible to rename a locked file. You should probably fix the real problem rather than the symptom, though.

erikkallen