Browse Source

Fixed a crash at memory allocation introduced by an unverified pull request.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/778/head
Dimitar Dobrev 9 years ago
parent
commit
73321c2e10
  1. 21
      src/Generator/Generators/CSharp/CSharpSources.cs

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

@ -2132,7 +2132,7 @@ namespace CppSharp.Generators.CSharp
if (@class.IsRefType && !@class.IsAbstract) if (@class.IsRefType && !@class.IsAbstract)
{ {
PushBlock(CSharpBlockKind.Method); PushBlock(CSharpBlockKind.Method);
WriteLine("private static void* __CopyValue({0} native)", @internal); WriteLine($"private static void* __CopyValue({@internal} native)");
WriteStartBraceIndent(); WriteStartBraceIndent();
var copyCtorMethod = @class.Methods.FirstOrDefault(method => var copyCtorMethod = @class.Methods.FirstOrDefault(method =>
method.IsCopyConstructor); method.IsCopyConstructor);
@ -2140,7 +2140,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(Marshal.SizeOf(sizeof({0})));", @internal); WriteLine($"var ret = Marshal.AllocHGlobal(Marshal.SizeOf(typeof({@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),
@ -2150,8 +2150,8 @@ namespace CppSharp.Generators.CSharp
} }
else else
{ {
WriteLine("var ret = Marshal.AllocHGlobal(Marshal.SizeOf(sizeof({0})));", @internal); WriteLine($"var ret = Marshal.AllocHGlobal(Marshal.SizeOf(typeof({@internal})));");
WriteLine("*({0}*) ret = native;", @internal); WriteLine($"*({@internal}*) ret = native;");
WriteLine("return ret.ToPointer();"); WriteLine("return ret.ToPointer();");
} }
WriteCloseBraceIndent(); WriteCloseBraceIndent();
@ -2166,12 +2166,12 @@ namespace CppSharp.Generators.CSharp
WriteStartBraceIndent(); WriteStartBraceIndent();
if (@class.IsRefType) if (@class.IsRefType)
{ {
WriteLine("{0} = true;", Helpers.OwnsNativeInstanceIdentifier); WriteLine($"{Helpers.OwnsNativeInstanceIdentifier} = true;");
WriteLine("NativeToManagedMap[{0}] = this;", Helpers.InstanceIdentifier); WriteLine($"NativeToManagedMap[{Helpers.InstanceIdentifier}] = this;");
} }
else else
{ {
WriteLine("{0} = native;", Helpers.InstanceField); WriteLine($"{Helpers.InstanceField} = native;");
} }
WriteCloseBraceIndent(); WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
@ -2594,10 +2594,9 @@ namespace CppSharp.Generators.CSharp
TypePrinter.PushContext(TypePrinterContextKind.Native); TypePrinter.PushContext(TypePrinterContextKind.Native);
var @internal = (@class.IsAbstractImpl ? @class.BaseClass : @class).Visit(TypePrinter); var @internal = (@class.IsAbstractImpl ? @class.BaseClass : @class).Visit(TypePrinter);
TypePrinter.PopContext(); TypePrinter.PopContext();
WriteLine("{0} = Marshal.AllocHGlobal(Marshal.SizeOf(sizeof({1})));", Helpers.InstanceIdentifier, WriteLine($"{Helpers.InstanceIdentifier} = Marshal.AllocHGlobal(Marshal.SizeOf(typeof({@internal})));");
@internal); WriteLine($"{Helpers.OwnsNativeInstanceIdentifier} = true;");
WriteLine("{0} = true;", Helpers.OwnsNativeInstanceIdentifier); WriteLine($"NativeToManagedMap[{Helpers.InstanceIdentifier}] = this;");
WriteLine("NativeToManagedMap[{0}] = this;", Helpers.InstanceIdentifier);
if (method.IsCopyConstructor) if (method.IsCopyConstructor)
{ {

Loading…
Cancel
Save