Visual-studio – get “file is used by another process” errors when I debug within Visual Studio

visual studio

Using Visual Studio 2010 beta, when I run my application within the IDE for debugging, it works perfectly the first time. However, after closing the debug session, either by closing the application or clicking the stop debugging button, all subsequent attempts to debug the application fail with:

Error 1 Unable to copy file "obj\Debug\Application.dll"
to
"bin\Debug\Application.dll".
The process cannot access the file
'bin\Debug\Application.dll'
because it is being used by another
process.

Handle.exe from SysInternals does show handles open, but even if I close the handles, the error doesn't go away. Any attempts to delete the file manually result in an "Access Denied" error message.

To fix this, I have to completely restart Visual Studio, afterwhich the Debug session will work once and stop again.

I'm not entirely sure when this started happening, but I'm pretty sure it's fairly recently.

UPDATE: After I force close the handles on Application.dll, I get the following error from VS:

Error 1 Unable to copy file
"obj\Debug\Application.dll"
to
"bin\Debug\Application.dll".
The requested operation cannot be
performed on a file with a user-mapped
section open.

What the heck is a "user-mapped section"??

UPDATE 2: It appears that this problem occurs when I have a Form open in Design view when trying to debug. I'm going to do some more troubleshooting and then post my results.

UPDATE 3: I think I've narrowed it down to a form using a UserControl.

Best Answer

To be honest with you, it sounds like a bug in VS2010. For some reason it isn't closing the open handles when the debugger stops. Killing the VS process automatically closes those handles, allowing you to access the file again. As a work around, you might look at unlocker it's free and works exceptionally well. I know that's not a great answer, but it should be faster than restarting VS. You might to consider sending a bug report too...

Unlocker doesn't work on 64-bit OS, LockHunter does though.