Skip to content

Commit

Permalink
Fix memory leaks for return values and some out params. (#1200)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
jkoritzinsky authored Jun 3, 2021
1 parent 41bf5d1 commit b11a26a
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
2 changes: 1 addition & 1 deletion DllImportGenerator/DllImportGenerator/StubCodeGenerator.cs
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 b11a26a

Please sign in to comment.