Browse Source

Merge pull request #347 from ddobrev/master

Fixed a memory problem in generated code. Mapped void* to IntPtr
pull/350/head
João Matos 11 years ago
parent
commit
349f133397
  1. 20
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  2. 3
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs

20
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -43,6 +43,8 @@ namespace CppSharp.Generators.CSharp @@ -43,6 +43,8 @@ namespace CppSharp.Generators.CSharp
public const string InstanceIdentifier = "__Instance";
public const string AllocatedWithHGlobalIdentifier = "__allocatedWithHGlobal";
public static string GetAccess(AccessSpecifier accessSpecifier)
{
switch (accessSpecifier)
@ -1861,8 +1863,14 @@ namespace CppSharp.Generators.CSharp @@ -1861,8 +1863,14 @@ namespace CppSharp.Generators.CSharp
}
}
}
}
if (@class.IsRefType)
{
WriteLine("if ({0})", Helpers.AllocatedWithHGlobalIdentifier);
WriteStartBraceIndent();
WriteLine("Marshal.FreeHGlobal({0});", Helpers.InstanceIdentifier);
WriteCloseBraceIndent();
}
if (hasBaseClass)
@ -1874,6 +1882,13 @@ namespace CppSharp.Generators.CSharp @@ -1874,6 +1882,13 @@ namespace CppSharp.Generators.CSharp
private void GenerateNativeConstructor(Class @class)
{
if (@class.IsRefType)
{
PushBlock(CSharpBlockKind.Field);
WriteLine("private readonly bool {0};", Helpers.AllocatedWithHGlobalIdentifier);
PopBlock(NewLineKind.BeforeNextBlock);
}
PushBlock(CSharpBlockKind.Method);
string className = @class.Name;
string safeIdentifier = className;
@ -1975,6 +1990,10 @@ namespace CppSharp.Generators.CSharp @@ -1975,6 +1990,10 @@ namespace CppSharp.Generators.CSharp
WriteLine("internal {0}({1}.Internal native)", safeIdentifier, className);
WriteLineIndent(@class.IsRefType ? ": this(__CopyValue(native))" : ": this(&native)", className);
WriteStartBraceIndent();
if (@class.IsRefType)
{
WriteLine("{0} = true;", Helpers.AllocatedWithHGlobalIdentifier);
}
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
}
@ -2240,6 +2259,7 @@ namespace CppSharp.Generators.CSharp @@ -2240,6 +2259,7 @@ namespace CppSharp.Generators.CSharp
{
WriteLine("{0} = Marshal.AllocHGlobal({1});", Helpers.InstanceIdentifier,
@class.Layout.Size);
WriteLine("{0} = true;", Helpers.AllocatedWithHGlobalIdentifier);
if (method.IsCopyConstructor)
{

3
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -220,7 +220,8 @@ namespace CppSharp.Generators.CSharp @@ -220,7 +220,8 @@ namespace CppSharp.Generators.CSharp
if (isManagedContext && param != null && (param.IsOut || param.IsInOut))
return pointee.Visit(this, quals);
if (ContextKind == CSharpTypePrinterContextKind.GenericDelegate)
if (ContextKind == CSharpTypePrinterContextKind.GenericDelegate ||
pointee.IsPrimitiveType(PrimitiveType.Void))
return "global::System.IntPtr";
return pointee.Visit(this, quals) + "*";

Loading…
Cancel
Save