Visual Studio 2010 kills (there is no other word) data in one of the arguments of the function in the unsafe block. What could cause this error? The following message shows
In my case, I was working on a web api project and although the project was set correctly to full debug, I was still seeing this error every time I attached to the IIS process I was trying to debug. Then I realized the publish profile was set to use the Release configuration. So one more place to check is your publish profile if you're using the 'Publish' feature of your dotnet web api project.
If you compile with optimizations enabled, then many variables will be removed; for example:
SomeType value = GetValue();
DoSomething(value);
here the local variable value
would typically get removed, keeping the value on the stack instead - a bit like as if you had written:
DoSomething(GetValue());
Also, if a return value isn't used at all, then it will be dropped via "pop" (rather than stored in a local via "stloc", and again; the local will not exist).
Because of this, in such a build the debugger can't get the current value of value
because it doesn't exist - it only exists for the brief instant between GetValue()
and DoSomething(...)
.
So; if you want to debug... don't use a release build! or at least, disable optimizations while you debug.
I found that I had the same problem when I was running a project and debugging by attaching to an IIS process. I also was running in Debug mode with optimizations turned off. While I thought the code compiled fine, when I detached and tried to compile, one of the references was not found. This was due to another developer here that made modifications and changed the location of the reference. The reference did not show up with the alert symbol, so I thought everything was fine until I did the compilation. Once fixing the reference and running again it worked.
In Visual Studio 2012:
Go to the project properties -> Debug -> Uncheck "Enable the Visual Studio hosting process"
In visual Studio 2017 goto Debug->Option then check Debugging->general-> and check this option
I had the same issue. Tried all the above and found I also had to delete everything inside {PROJECT_ROOT}\bin\Release\netcoreapp2.2 and {PROJECT_ROOT}\obj\Release\netcoreapp2.2 for my project. Its definitely releated to publishing because although I use Deployment tools / bitbucket on my Azure Web App, I did try the Build >> Publish >> Publish to Azure because I wanted to inspect which files were actually deployed.