Skip to content

Commit

Permalink
Fix memory leaks for return values and some out params. (dotnet/runti…
Browse files Browse the repository at this point in the history
…melab#1200)

Fix some memory leaks we had where we didn't generate code to free native resources for out parameters that use custom native type marshalling or all return values.

Also set a flag in the csproj that enables a nice debugging innerloop option for roslyn components in the csproj settings (in our case running the generator on the integration tests)

Commit migrated from dotnet/runtimelab@b11a26a
  • Loading branch information
jkoritzinsky authored Jun 3, 2021
1 parent 51aaa63 commit 2ee7b3e
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<LangVersion>Preview</LangVersion>
<Nullable>enable</Nullable>
<RootNamespace>Microsoft.Interop</RootNamespace>
<IsRoslynComponent>true</IsRoslynComponent>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeCont
}
break;
case StubCodeContext.Stage.Cleanup:
if (info.RefKind != RefKind.Out && _hasFreeNative)
if (_hasFreeNative)
{
// <marshalerIdentifier>.FreeNative();
yield return ExpressionStatement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeCont
}
break;
case StubCodeContext.Stage.Cleanup:
if (!info.IsByRef || info.RefKind == RefKind.In)
if (!info.IsManagedReturnPosition && (!info.IsByRef || info.RefKind == RefKind.In))
{
yield return IfStatement(
IdentifierName(addRefdIdentifier),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public BlockSyntax GenerateSyntax()
int initialCount = statements.Count;
this.CurrentStage = stage;

if (!invokeReturnsVoid && (stage == Stage.Setup || stage == Stage.Unmarshal || stage == Stage.GuaranteedUnmarshal))
if (!invokeReturnsVoid && (stage is Stage.Setup or Stage.Unmarshal or Stage.GuaranteedUnmarshal or Stage.Cleanup))
{
// Handle setup and unmarshalling for return
var retStatements = retMarshaller.Generator.Generate(retMarshaller.TypeInfo, this);
Expand Down

0 comments on commit 2ee7b3e

Please sign in to comment.