As commented by FCin
, , , # - Dangling Pointer. , . , FooUnsafe .
JuanR
fixed Statement ( #)
.
, Interwebs
private static unsafe void Main()
{
Console.WriteLine($"Total Memory: {GC.GetTotalMemory(false)}");
var arr = new int[100000];
Console.WriteLine($"Total Memory after new : {GC.GetTotalMemory(false)}");
try
{
fixed (int* p = arr)
{
*p = 1;
throw new Exception("rah");
}
}
catch
{
}
Console.WriteLine($"Generation: {GC.GetGeneration(arr)}, Total Memory: {GC.GetTotalMemory(false)}");
arr = null;
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));
Console.Read();
}
Total Memory: 29948
Total Memory after new: 438172
Generation: 2, Total Memory: 438172
Total Memory: 29824
IL finally ldnull
.try
{
// [23 14 - 23 26]
IL_0043: ldloc.0 // arr
IL_0044: dup
IL_0045: stloc.2 // V_2
IL_0046: brfalse.s IL_004d
IL_0048: ldloc.2 // V_2
IL_0049: ldlen
IL_004a: conv.i4
IL_004b: brtrue.s IL_0052
IL_004d: ldc.i4.0
IL_004e: conv.u
IL_004f: stloc.1 // p
IL_0050: br.s IL_005b
IL_0052: ldloc.2 // V_2
IL_0053: ldc.i4.0
IL_0054: ldelema [mscorlib]System.Int32
IL_0059: conv.u
IL_005a: stloc.1 // p
...
} // end of .try
finally
{
IL_006a: ldnull
IL_006b: stloc.2 // V_2
IL_006c: endfinally
} // end of finally
: , .
LocalRewriter_FixedStatement.cs Roslyn
if (IsInTryBlock(node) || HasGotoOut(rewrittenBody))
{
i.e ,
private static unsafe void test(int[] arr)
{
fixed (int* p = arr)
{
*p = 1;
}
}
.method private hidebysig static void
test(
int32[] arr
) cil managed
{
.maxstack 2
.locals init (
[0] int32* p,
[1] int32[] pinned V_1
)
...
IL_001e: ldnull
IL_001f: stloc.1
IL_0020: ret
}
ECMA-335 (CLI)
II.7.1.2 , (§II.15.4.1.3). , VES , . , CLI , , , .
[: , . , , , . ]
VES = CLI = CTS = Common Type
, JITer CLR , GC
, GC . GC , , , . , , . GC Compact Phase, , .
, , , GC .

, ( ), ary , , GC .
. , , , ,