Skip to content

Commit

Permalink
Keep this alive across the native call for VTable-based source-gene…
Browse files Browse the repository at this point in the history
…ration (#85723)

* Keep `this` alive across the native call for VTable-based source-generation
  • Loading branch information
jkoritzinsky authored May 4, 2023
1 parent cc4a477 commit 91cdf05
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,19 @@ public BlockSyntax GenerateStubBody(int index, ImmutableArray<FunctionPointerUnm
}

tryStatements.AddRange(statements.NotifyForSuccessfulInvoke);

// Keep the this object alive across the native call, similar to how we handle marshalling managed delegates.
// We do this right after the NotifyForSuccessfulInvoke phase as that phase is where the delegate objects are kept alive.
// If we ever move the "this" object handling out of this type, we'll move the handling to be emitted in that phase.
// GC.KeepAlive(this);
tryStatements.Add(
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
ParseTypeName(TypeNames.System_GC),
IdentifierName("KeepAlive")),
ArgumentList(SingletonSeparatedList(Argument(ThisExpression()))))));

tryStatements.AddRange(statements.Unmarshal);

List<StatementSyntax> allStatements = setupStatements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public static class TypeNames

public const string System_Exception = "System.Exception";

public const string System_GC = "System.GC";

public const string System_Type = "System.Type";

public const string System_Int16 = "System.Int16";
Expand Down

0 comments on commit 91cdf05

Please sign in to comment.