Skip to content

Commit dde6d74

Browse files
committed
Remove the copying of returned values
This improves performance and more importantly, fixes a memory leak caused by the original value not being destroyed. Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 8815307 commit dde6d74

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

src/Generator/Generators/CSharp/CSharpMarshal.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,13 @@ public override bool VisitClassDecl(Class @class)
289289

290290
if (returnType.IsAddress())
291291
Context.Return.Write(HandleReturnedPointer(@class, qualifiedClass.Type));
292-
else
293-
Context.Return.Write($"{qualifiedClass}.{Helpers.CreateInstanceIdentifier}({Context.ReturnVarName})");
292+
else if (Context.MarshalKind == MarshalKind.NativeField ||
293+
Context.Function?.HasIndirectReturnTypeParameter == true ||
294+
Context.Function?.OperatorKind == CXXOperatorKind.Subscript)
295+
Context.Return.Write($@"{qualifiedClass}.{Helpers.CreateInstanceIdentifier}({
296+
Context.ReturnVarName})");
297+
else Context.Return.Write($@"{qualifiedClass}.{Helpers.CreateInstanceIdentifier}(new {
298+
typePrinter.IntPtrType}(&{Context.ReturnVarName}))");
294299

295300
return true;
296301
}

src/Generator/Generators/CSharp/CSharpSources.cs

+6-9
Original file line numberDiff line numberDiff line change
@@ -2808,6 +2808,7 @@ public void GenerateFunctionCall(string functionName, List<Parameter> parameters
28082808

28092809
var originalFunction = function.OriginalFunction ?? function;
28102810

2811+
var names = new List<string>();
28112812
if (originalFunction.HasIndirectReturnTypeParameter)
28122813
{
28132814
var indirectRetType = originalFunction.Parameters.First(
@@ -2825,8 +2826,8 @@ public void GenerateFunctionCall(string functionName, List<Parameter> parameters
28252826
Class retClass;
28262827
type.TryGetClass(out retClass);
28272828
var @class = retClass.OriginalClass ?? retClass;
2828-
WriteLine($@"var {Helpers.ReturnIdentifier} = new {
2829-
TypePrinter.PrintNative(@class)}();");
2829+
WriteLine($@"var {Helpers.ReturnIdentifier} = Marshal.AllocHGlobal({
2830+
@class.Layout.GetSize()});");
28302831
}
28312832
else
28322833
{
@@ -2839,13 +2840,15 @@ public void GenerateFunctionCall(string functionName, List<Parameter> parameters
28392840

28402841
WriteLine("{0} {1};", typeMap.CSharpSignatureType(typePrinterContext),
28412842
Helpers.ReturnIdentifier);
2843+
names.Add($"new IntPtr(&{Helpers.ReturnIdentifier})");
28422844
}
28432845
else
28442846
WriteLine("var {0} = {1};", construct);
28452847
}
2848+
if (names.Count == 0)
2849+
names.Add(Helpers.ReturnIdentifier);
28462850
}
28472851

2848-
var names = new List<string>();
28492852
foreach (var param in @params)
28502853
{
28512854
if (param.Param == operatorParam && needsInstance)
@@ -2862,12 +2865,6 @@ public void GenerateFunctionCall(string functionName, List<Parameter> parameters
28622865

28632866
var needsFixedThis = needsInstance && isValueType;
28642867

2865-
if (originalFunction.HasIndirectReturnTypeParameter)
2866-
{
2867-
var name = string.Format("new IntPtr(&{0})", Helpers.ReturnIdentifier);
2868-
names.Insert(0, name);
2869-
}
2870-
28712868
if (needsInstance)
28722869
{
28732870
var instanceIndex = GetInstanceParamIndex(method);

src/Generator/Types/Std/Stdlib.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,7 @@ public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
382382
ctx.MarshalKind == MarshalKind.ReturnVariableArray;
383383
ctx.Before.WriteLine($@"var {varBasicString} = {
384384
basicString.Visit(typePrinter)}.{Helpers.CreateInstanceIdentifier}({
385-
(usePointer ? string.Empty : $"new {typePrinter.IntPtrType}(&")}{
386-
ctx.ReturnVarName}{(usePointer ? string.Empty : ")")});");
385+
ctx.ReturnVarName});");
387386
string @string = $"{qualifiedBasicString}Extensions.{data.Name}({varBasicString})";
388387
if (usePointer)
389388
{

0 commit comments

Comments
 (0)