I have an XNA 3.0 project that compiled just fine in VS2008, but that gives compile errors in VS2010 (with XNA 4.0 CTP). The error:
Cannot use fixed local 'depthPtr' inside an anonymous method, lambda expression, or query expression
depthPtr
is a fixed float*
into an array, that is used inside a Parallel.For
lambda expression from System.Threading
. As I said, this compiled and ran just fine on VS2008, but it does not on VS2010, even when targeting .NET 3.5.
Has this changed in .NET 4.0, and even so, shouldn't it still compile when I choose .NET 3.5 as the target framework? Searching for the term "Cannot use fixed local" yields exactly one (useless) result, both in Google and Bing.
If this has changed, what is the reason for this? I can imagine capturing a fixed
pointer-type in a closure could get a bit weird, is that why? So I'm guessing this is bad practice? And before anyone asks: no, the use of pointers is not absolutely critical here. I would still like to know though :)
EDIT: As requested, a code sample (not from my program, obviously) that reproduces the error:
static unsafe void Main(string[] args)
{
float[] array = new float[10];
fixed (float* ptr = array)
{
Parallel.For(0, 10, i =>
{
ptr[i] = i;
});
}
}
The above compiles in VS2008 (well, aside from the reference to Parallel
, but any other lambda expression will do), but does not in VS2010.