Browse Source

Let the marshal determine the size of the structure

Signed-off-by: Alin Gherman <Alin Gherman>
pull/770/head
Alin Gherman 9 years ago
parent
commit
66bbc388e1
  1. 11
      src/Generator/Generators/CSharp/CSharpSources.cs

11
src/Generator/Generators/CSharp/CSharpSources.cs

@ -2126,7 +2126,7 @@ namespace CppSharp.Generators.CSharp
copyCtorMethod.IsGenerated) copyCtorMethod.IsGenerated)
{ {
// Allocate memory for a new native object and call the ctor. // Allocate memory for a new native object and call the ctor.
WriteLine("var ret = Marshal.AllocHGlobal({0});", @class.Layout.Size); WriteLine("var ret = Marshal.AllocHGlobal(Marshal.SizeOf(sizeof({0})));", @internal);
TypePrinter.PushContext(TypePrinterContextKind.Native); TypePrinter.PushContext(TypePrinterContextKind.Native);
WriteLine($"{@class.Visit(TypePrinter)}.{GetFunctionNativeIdentifier(copyCtorMethod)}(ret, new global::System.IntPtr(&native));", WriteLine($"{@class.Visit(TypePrinter)}.{GetFunctionNativeIdentifier(copyCtorMethod)}(ret, new global::System.IntPtr(&native));",
@class.Visit(TypePrinter), @class.Visit(TypePrinter),
@ -2136,7 +2136,7 @@ namespace CppSharp.Generators.CSharp
} }
else else
{ {
WriteLine("var ret = Marshal.AllocHGlobal({0});", @class.Layout.Size); WriteLine("var ret = Marshal.AllocHGlobal(Marshal.SizeOf(sizeof({0})));", @internal);
WriteLine("*({0}*) ret = native;", @internal); WriteLine("*({0}*) ret = native;", @internal);
WriteLine("return ret.ToPointer();"); WriteLine("return ret.ToPointer();");
} }
@ -2577,8 +2577,11 @@ namespace CppSharp.Generators.CSharp
private void GenerateClassConstructor(Method method, Class @class) private void GenerateClassConstructor(Method method, Class @class)
{ {
WriteLine("{0} = Marshal.AllocHGlobal({1});", Helpers.InstanceIdentifier, TypePrinter.PushContext(TypePrinterContextKind.Native);
@class.Layout.Size); var @internal = (@class.IsAbstractImpl ? @class.BaseClass : @class).Visit(TypePrinter);
TypePrinter.PopContext();
WriteLine("{0} = Marshal.AllocHGlobal(Marshal.SizeOf(sizeof({1})));", Helpers.InstanceIdentifier,
@internal);
WriteLine("{0} = true;", Helpers.OwnsNativeInstanceIdentifier); WriteLine("{0} = true;", Helpers.OwnsNativeInstanceIdentifier);
WriteLine("NativeToManagedMap[{0}] = this;", Helpers.InstanceIdentifier); WriteLine("NativeToManagedMap[{0}] = this;", Helpers.InstanceIdentifier);

Loading…
Cancel
Save