Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jkotas committed May 17, 2024
1 parent e841c63 commit 3d21eb6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ public static unsafe int DoSomething()
byte[] bytes = new byte[] { 1, 2, 3 };
fixed (byte* bytePtr = bytes)
{
// Take address of the local variable to prevent it from being optimized out by Roslyn
GC.KeepAlive((nint)(&bytePtr));

return *bytePtr;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,17 @@ private static void MethodBodyExample(object arg)
catch (Exception ex)
{
Console.WriteLine(ex.Message);
// Reference local variables to prevent them from being optimized out by Roslyn
GC.KeepAlive(ex);
}
finally
{
var1 = 3;
var2 = "I am a new string!";
}
// Reference local variables to prevent them from being optimized out by Roslyn
GC.KeepAlive((object)var1);
GC.KeepAlive(var2);
}
}
}

0 comments on commit 3d21eb6

Please sign in to comment.