From f97ad43ce40f3ef14ffa8f0a7a33c1dfa235aa53 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Thu, 7 Oct 2021 00:12:58 +0300 Subject: [PATCH] Generate valid C# for base types with public fields of const char* Signed-off-by: Dimitar Dobrev --- src/AST/ClassExtensions.cs | 4 ++ .../CppSharp.CppParser.cs | 18 +++++++++ .../i686-pc-win32-msvc/CppSharp.CppParser.cs | 18 +++++++++ .../CppSharp.CppParser.cs | 18 +++++++++ .../CppSharp.CppParser.cs | 18 +++++++++ .../x86_64-linux-gnu/CppSharp.CppParser.cs | 18 +++++++++ .../CppSharp.CppParser.cs | 18 +++++++++ .../Generators/CSharp/CSharpSources.cs | 39 +++++++++---------- 8 files changed, 130 insertions(+), 21 deletions(-) diff --git a/src/AST/ClassExtensions.cs b/src/AST/ClassExtensions.cs index 79142a54..5b1fabc3 100644 --- a/src/AST/ClassExtensions.cs +++ b/src/AST/ClassExtensions.cs @@ -242,6 +242,10 @@ namespace CppSharp.AST b => b.Class).Any(HasDependentValueFieldInLayout); } + public static IEnumerable GetConstCharFieldProperties(this Class @class) => + @class.Properties.Where(p => p.HasGetter && p.HasSetter && + p.Field?.QualifiedType.Type.IsConstCharString() == true); + private static bool IsValueDependent(Type type) { var desugared = type.Desugar(); diff --git a/src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/CppSharp.CppParser.cs b/src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/CppSharp.CppParser.cs index 83441d97..c3d4673c 100644 --- a/src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/CppSharp.CppParser.cs +++ b/src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/CppSharp.CppParser.cs @@ -16620,6 +16620,7 @@ namespace CppSharp internal static extern void cctor(__IntPtr __instance, __IntPtr _0); } + private bool __name_OwnsNativeMemory = false; internal static new LabelStmt __CreateInstance(__IntPtr native, bool skipVTables = false) { return new LabelStmt(native.ToPointer(), skipVTables); @@ -16677,6 +16678,8 @@ namespace CppSharp __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; *((global::CppSharp.Parser.AST.LabelStmt.__Internal*) __Instance) = *((global::CppSharp.Parser.AST.LabelStmt.__Internal*) _0.__Instance); + if (_0.__name_OwnsNativeMemory) + this.Name = _0.Name; } public global::CppSharp.Parser.SourceLocation IdentLoc @@ -16715,6 +16718,9 @@ namespace CppSharp set { + if (__name_OwnsNativeMemory) + Marshal.FreeHGlobal(((__Internal*)__Instance)->name); + __name_OwnsNativeMemory = true; if (value == null) { ((__Internal*)__Instance)->name = global::System.IntPtr.Zero; @@ -25167,6 +25173,7 @@ namespace CppSharp internal static extern void cctor(__IntPtr __instance, __IntPtr _0); } + private bool __castKindName_OwnsNativeMemory = false; internal static new CastExpr __CreateInstance(__IntPtr native, bool skipVTables = false) { return new CastExpr(native.ToPointer(), skipVTables); @@ -25233,6 +25240,8 @@ namespace CppSharp __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; *((global::CppSharp.Parser.AST.CastExpr.__Internal*) __Instance) = *((global::CppSharp.Parser.AST.CastExpr.__Internal*) _0.__Instance); + if (_0.__castKindName_OwnsNativeMemory) + this.CastKindName = _0.CastKindName; } public static implicit operator global::CppSharp.Parser.AST.CastExpr(global::CppSharp.Parser.AST.StmtClass klass) @@ -25276,6 +25285,9 @@ namespace CppSharp set { + if (__castKindName_OwnsNativeMemory) + Marshal.FreeHGlobal(((__Internal*)__Instance)->castKindName); + __castKindName_OwnsNativeMemory = true; if (value == null) { ((__Internal*)__Instance)->castKindName = global::System.IntPtr.Zero; @@ -30958,6 +30970,7 @@ namespace CppSharp internal static extern void cctor(__IntPtr __instance, __IntPtr _0); } + private bool __castName_OwnsNativeMemory = false; internal static new CXXNamedCastExpr __CreateInstance(__IntPtr native, bool skipVTables = false) { return new CXXNamedCastExpr(native.ToPointer(), skipVTables); @@ -31024,6 +31037,8 @@ namespace CppSharp __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; *((global::CppSharp.Parser.AST.CXXNamedCastExpr.__Internal*) __Instance) = *((global::CppSharp.Parser.AST.CXXNamedCastExpr.__Internal*) _0.__Instance); + if (_0.__castName_OwnsNativeMemory) + this.CastName = _0.CastName; } public static implicit operator global::CppSharp.Parser.AST.CXXNamedCastExpr(global::CppSharp.Parser.AST.StmtClass klass) @@ -31040,6 +31055,9 @@ namespace CppSharp set { + if (__castName_OwnsNativeMemory) + Marshal.FreeHGlobal(((__Internal*)__Instance)->castName); + __castName_OwnsNativeMemory = true; if (value == null) { ((__Internal*)__Instance)->castName = global::System.IntPtr.Zero; diff --git a/src/CppParser/Bindings/CSharp/i686-pc-win32-msvc/CppSharp.CppParser.cs b/src/CppParser/Bindings/CSharp/i686-pc-win32-msvc/CppSharp.CppParser.cs index 3702f83d..77d62661 100644 --- a/src/CppParser/Bindings/CSharp/i686-pc-win32-msvc/CppSharp.CppParser.cs +++ b/src/CppParser/Bindings/CSharp/i686-pc-win32-msvc/CppSharp.CppParser.cs @@ -16622,6 +16622,7 @@ namespace CppSharp internal static extern __IntPtr cctor(__IntPtr __instance, __IntPtr _0); } + private bool __name_OwnsNativeMemory = false; internal static new LabelStmt __CreateInstance(__IntPtr native, bool skipVTables = false) { return new LabelStmt(native.ToPointer(), skipVTables); @@ -16679,6 +16680,8 @@ namespace CppSharp __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; *((global::CppSharp.Parser.AST.LabelStmt.__Internal*) __Instance) = *((global::CppSharp.Parser.AST.LabelStmt.__Internal*) _0.__Instance); + if (_0.__name_OwnsNativeMemory) + this.Name = _0.Name; } public global::CppSharp.Parser.SourceLocation IdentLoc @@ -16717,6 +16720,9 @@ namespace CppSharp set { + if (__name_OwnsNativeMemory) + Marshal.FreeHGlobal(((__Internal*)__Instance)->name); + __name_OwnsNativeMemory = true; if (value == null) { ((__Internal*)__Instance)->name = global::System.IntPtr.Zero; @@ -25171,6 +25177,7 @@ namespace CppSharp internal static extern __IntPtr cctor(__IntPtr __instance, __IntPtr _0); } + private bool __castKindName_OwnsNativeMemory = false; internal static new CastExpr __CreateInstance(__IntPtr native, bool skipVTables = false) { return new CastExpr(native.ToPointer(), skipVTables); @@ -25237,6 +25244,8 @@ namespace CppSharp __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; *((global::CppSharp.Parser.AST.CastExpr.__Internal*) __Instance) = *((global::CppSharp.Parser.AST.CastExpr.__Internal*) _0.__Instance); + if (_0.__castKindName_OwnsNativeMemory) + this.CastKindName = _0.CastKindName; } public static implicit operator global::CppSharp.Parser.AST.CastExpr(global::CppSharp.Parser.AST.StmtClass klass) @@ -25280,6 +25289,9 @@ namespace CppSharp set { + if (__castKindName_OwnsNativeMemory) + Marshal.FreeHGlobal(((__Internal*)__Instance)->castKindName); + __castKindName_OwnsNativeMemory = true; if (value == null) { ((__Internal*)__Instance)->castKindName = global::System.IntPtr.Zero; @@ -30963,6 +30975,7 @@ namespace CppSharp internal static extern __IntPtr cctor(__IntPtr __instance, __IntPtr _0); } + private bool __castName_OwnsNativeMemory = false; internal static new CXXNamedCastExpr __CreateInstance(__IntPtr native, bool skipVTables = false) { return new CXXNamedCastExpr(native.ToPointer(), skipVTables); @@ -31029,6 +31042,8 @@ namespace CppSharp __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; *((global::CppSharp.Parser.AST.CXXNamedCastExpr.__Internal*) __Instance) = *((global::CppSharp.Parser.AST.CXXNamedCastExpr.__Internal*) _0.__Instance); + if (_0.__castName_OwnsNativeMemory) + this.CastName = _0.CastName; } public static implicit operator global::CppSharp.Parser.AST.CXXNamedCastExpr(global::CppSharp.Parser.AST.StmtClass klass) @@ -31045,6 +31060,9 @@ namespace CppSharp set { + if (__castName_OwnsNativeMemory) + Marshal.FreeHGlobal(((__Internal*)__Instance)->castName); + __castName_OwnsNativeMemory = true; if (value == null) { ((__Internal*)__Instance)->castName = global::System.IntPtr.Zero; diff --git a/src/CppParser/Bindings/CSharp/x86_64-apple-darwin12.4.0/CppSharp.CppParser.cs b/src/CppParser/Bindings/CSharp/x86_64-apple-darwin12.4.0/CppSharp.CppParser.cs index ead86835..883a8326 100644 --- a/src/CppParser/Bindings/CSharp/x86_64-apple-darwin12.4.0/CppSharp.CppParser.cs +++ b/src/CppParser/Bindings/CSharp/x86_64-apple-darwin12.4.0/CppSharp.CppParser.cs @@ -16619,6 +16619,7 @@ namespace CppSharp internal static extern void cctor(__IntPtr __instance, __IntPtr _0); } + private bool __name_OwnsNativeMemory = false; internal static new LabelStmt __CreateInstance(__IntPtr native, bool skipVTables = false) { return new LabelStmt(native.ToPointer(), skipVTables); @@ -16676,6 +16677,8 @@ namespace CppSharp __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; *((global::CppSharp.Parser.AST.LabelStmt.__Internal*) __Instance) = *((global::CppSharp.Parser.AST.LabelStmt.__Internal*) _0.__Instance); + if (_0.__name_OwnsNativeMemory) + this.Name = _0.Name; } public global::CppSharp.Parser.SourceLocation IdentLoc @@ -16714,6 +16717,9 @@ namespace CppSharp set { + if (__name_OwnsNativeMemory) + Marshal.FreeHGlobal(((__Internal*)__Instance)->name); + __name_OwnsNativeMemory = true; if (value == null) { ((__Internal*)__Instance)->name = global::System.IntPtr.Zero; @@ -25166,6 +25172,7 @@ namespace CppSharp internal static extern void cctor(__IntPtr __instance, __IntPtr _0); } + private bool __castKindName_OwnsNativeMemory = false; internal static new CastExpr __CreateInstance(__IntPtr native, bool skipVTables = false) { return new CastExpr(native.ToPointer(), skipVTables); @@ -25232,6 +25239,8 @@ namespace CppSharp __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; *((global::CppSharp.Parser.AST.CastExpr.__Internal*) __Instance) = *((global::CppSharp.Parser.AST.CastExpr.__Internal*) _0.__Instance); + if (_0.__castKindName_OwnsNativeMemory) + this.CastKindName = _0.CastKindName; } public static implicit operator global::CppSharp.Parser.AST.CastExpr(global::CppSharp.Parser.AST.StmtClass klass) @@ -25275,6 +25284,9 @@ namespace CppSharp set { + if (__castKindName_OwnsNativeMemory) + Marshal.FreeHGlobal(((__Internal*)__Instance)->castKindName); + __castKindName_OwnsNativeMemory = true; if (value == null) { ((__Internal*)__Instance)->castKindName = global::System.IntPtr.Zero; @@ -30957,6 +30969,7 @@ namespace CppSharp internal static extern void cctor(__IntPtr __instance, __IntPtr _0); } + private bool __castName_OwnsNativeMemory = false; internal static new CXXNamedCastExpr __CreateInstance(__IntPtr native, bool skipVTables = false) { return new CXXNamedCastExpr(native.ToPointer(), skipVTables); @@ -31023,6 +31036,8 @@ namespace CppSharp __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; *((global::CppSharp.Parser.AST.CXXNamedCastExpr.__Internal*) __Instance) = *((global::CppSharp.Parser.AST.CXXNamedCastExpr.__Internal*) _0.__Instance); + if (_0.__castName_OwnsNativeMemory) + this.CastName = _0.CastName; } public static implicit operator global::CppSharp.Parser.AST.CXXNamedCastExpr(global::CppSharp.Parser.AST.StmtClass klass) @@ -31039,6 +31054,9 @@ namespace CppSharp set { + if (__castName_OwnsNativeMemory) + Marshal.FreeHGlobal(((__Internal*)__Instance)->castName); + __castName_OwnsNativeMemory = true; if (value == null) { ((__Internal*)__Instance)->castName = global::System.IntPtr.Zero; diff --git a/src/CppParser/Bindings/CSharp/x86_64-linux-gnu-cxx11abi/CppSharp.CppParser.cs b/src/CppParser/Bindings/CSharp/x86_64-linux-gnu-cxx11abi/CppSharp.CppParser.cs index 40963315..b116a4e2 100644 --- a/src/CppParser/Bindings/CSharp/x86_64-linux-gnu-cxx11abi/CppSharp.CppParser.cs +++ b/src/CppParser/Bindings/CSharp/x86_64-linux-gnu-cxx11abi/CppSharp.CppParser.cs @@ -16619,6 +16619,7 @@ namespace CppSharp internal static extern void cctor(__IntPtr __instance, __IntPtr _0); } + private bool __name_OwnsNativeMemory = false; internal static new LabelStmt __CreateInstance(__IntPtr native, bool skipVTables = false) { return new LabelStmt(native.ToPointer(), skipVTables); @@ -16676,6 +16677,8 @@ namespace CppSharp __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; *((global::CppSharp.Parser.AST.LabelStmt.__Internal*) __Instance) = *((global::CppSharp.Parser.AST.LabelStmt.__Internal*) _0.__Instance); + if (_0.__name_OwnsNativeMemory) + this.Name = _0.Name; } public global::CppSharp.Parser.SourceLocation IdentLoc @@ -16714,6 +16717,9 @@ namespace CppSharp set { + if (__name_OwnsNativeMemory) + Marshal.FreeHGlobal(((__Internal*)__Instance)->name); + __name_OwnsNativeMemory = true; if (value == null) { ((__Internal*)__Instance)->name = global::System.IntPtr.Zero; @@ -25166,6 +25172,7 @@ namespace CppSharp internal static extern void cctor(__IntPtr __instance, __IntPtr _0); } + private bool __castKindName_OwnsNativeMemory = false; internal static new CastExpr __CreateInstance(__IntPtr native, bool skipVTables = false) { return new CastExpr(native.ToPointer(), skipVTables); @@ -25232,6 +25239,8 @@ namespace CppSharp __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; *((global::CppSharp.Parser.AST.CastExpr.__Internal*) __Instance) = *((global::CppSharp.Parser.AST.CastExpr.__Internal*) _0.__Instance); + if (_0.__castKindName_OwnsNativeMemory) + this.CastKindName = _0.CastKindName; } public static implicit operator global::CppSharp.Parser.AST.CastExpr(global::CppSharp.Parser.AST.StmtClass klass) @@ -25275,6 +25284,9 @@ namespace CppSharp set { + if (__castKindName_OwnsNativeMemory) + Marshal.FreeHGlobal(((__Internal*)__Instance)->castKindName); + __castKindName_OwnsNativeMemory = true; if (value == null) { ((__Internal*)__Instance)->castKindName = global::System.IntPtr.Zero; @@ -30957,6 +30969,7 @@ namespace CppSharp internal static extern void cctor(__IntPtr __instance, __IntPtr _0); } + private bool __castName_OwnsNativeMemory = false; internal static new CXXNamedCastExpr __CreateInstance(__IntPtr native, bool skipVTables = false) { return new CXXNamedCastExpr(native.ToPointer(), skipVTables); @@ -31023,6 +31036,8 @@ namespace CppSharp __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; *((global::CppSharp.Parser.AST.CXXNamedCastExpr.__Internal*) __Instance) = *((global::CppSharp.Parser.AST.CXXNamedCastExpr.__Internal*) _0.__Instance); + if (_0.__castName_OwnsNativeMemory) + this.CastName = _0.CastName; } public static implicit operator global::CppSharp.Parser.AST.CXXNamedCastExpr(global::CppSharp.Parser.AST.StmtClass klass) @@ -31039,6 +31054,9 @@ namespace CppSharp set { + if (__castName_OwnsNativeMemory) + Marshal.FreeHGlobal(((__Internal*)__Instance)->castName); + __castName_OwnsNativeMemory = true; if (value == null) { ((__Internal*)__Instance)->castName = global::System.IntPtr.Zero; diff --git a/src/CppParser/Bindings/CSharp/x86_64-linux-gnu/CppSharp.CppParser.cs b/src/CppParser/Bindings/CSharp/x86_64-linux-gnu/CppSharp.CppParser.cs index 674c0fa5..3abedf1c 100644 --- a/src/CppParser/Bindings/CSharp/x86_64-linux-gnu/CppSharp.CppParser.cs +++ b/src/CppParser/Bindings/CSharp/x86_64-linux-gnu/CppSharp.CppParser.cs @@ -16619,6 +16619,7 @@ namespace CppSharp internal static extern void cctor(__IntPtr __instance, __IntPtr _0); } + private bool __name_OwnsNativeMemory = false; internal static new LabelStmt __CreateInstance(__IntPtr native, bool skipVTables = false) { return new LabelStmt(native.ToPointer(), skipVTables); @@ -16676,6 +16677,8 @@ namespace CppSharp __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; *((global::CppSharp.Parser.AST.LabelStmt.__Internal*) __Instance) = *((global::CppSharp.Parser.AST.LabelStmt.__Internal*) _0.__Instance); + if (_0.__name_OwnsNativeMemory) + this.Name = _0.Name; } public global::CppSharp.Parser.SourceLocation IdentLoc @@ -16714,6 +16717,9 @@ namespace CppSharp set { + if (__name_OwnsNativeMemory) + Marshal.FreeHGlobal(((__Internal*)__Instance)->name); + __name_OwnsNativeMemory = true; if (value == null) { ((__Internal*)__Instance)->name = global::System.IntPtr.Zero; @@ -25166,6 +25172,7 @@ namespace CppSharp internal static extern void cctor(__IntPtr __instance, __IntPtr _0); } + private bool __castKindName_OwnsNativeMemory = false; internal static new CastExpr __CreateInstance(__IntPtr native, bool skipVTables = false) { return new CastExpr(native.ToPointer(), skipVTables); @@ -25232,6 +25239,8 @@ namespace CppSharp __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; *((global::CppSharp.Parser.AST.CastExpr.__Internal*) __Instance) = *((global::CppSharp.Parser.AST.CastExpr.__Internal*) _0.__Instance); + if (_0.__castKindName_OwnsNativeMemory) + this.CastKindName = _0.CastKindName; } public static implicit operator global::CppSharp.Parser.AST.CastExpr(global::CppSharp.Parser.AST.StmtClass klass) @@ -25275,6 +25284,9 @@ namespace CppSharp set { + if (__castKindName_OwnsNativeMemory) + Marshal.FreeHGlobal(((__Internal*)__Instance)->castKindName); + __castKindName_OwnsNativeMemory = true; if (value == null) { ((__Internal*)__Instance)->castKindName = global::System.IntPtr.Zero; @@ -30957,6 +30969,7 @@ namespace CppSharp internal static extern void cctor(__IntPtr __instance, __IntPtr _0); } + private bool __castName_OwnsNativeMemory = false; internal static new CXXNamedCastExpr __CreateInstance(__IntPtr native, bool skipVTables = false) { return new CXXNamedCastExpr(native.ToPointer(), skipVTables); @@ -31023,6 +31036,8 @@ namespace CppSharp __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; *((global::CppSharp.Parser.AST.CXXNamedCastExpr.__Internal*) __Instance) = *((global::CppSharp.Parser.AST.CXXNamedCastExpr.__Internal*) _0.__Instance); + if (_0.__castName_OwnsNativeMemory) + this.CastName = _0.CastName; } public static implicit operator global::CppSharp.Parser.AST.CXXNamedCastExpr(global::CppSharp.Parser.AST.StmtClass klass) @@ -31039,6 +31054,9 @@ namespace CppSharp set { + if (__castName_OwnsNativeMemory) + Marshal.FreeHGlobal(((__Internal*)__Instance)->castName); + __castName_OwnsNativeMemory = true; if (value == null) { ((__Internal*)__Instance)->castName = global::System.IntPtr.Zero; diff --git a/src/CppParser/Bindings/CSharp/x86_64-pc-win32-msvc/CppSharp.CppParser.cs b/src/CppParser/Bindings/CSharp/x86_64-pc-win32-msvc/CppSharp.CppParser.cs index aa2e6a41..0abff4f6 100644 --- a/src/CppParser/Bindings/CSharp/x86_64-pc-win32-msvc/CppSharp.CppParser.cs +++ b/src/CppParser/Bindings/CSharp/x86_64-pc-win32-msvc/CppSharp.CppParser.cs @@ -16623,6 +16623,7 @@ namespace CppSharp internal static extern __IntPtr cctor(__IntPtr __instance, __IntPtr _0); } + private bool __name_OwnsNativeMemory = false; internal static new LabelStmt __CreateInstance(__IntPtr native, bool skipVTables = false) { return new LabelStmt(native.ToPointer(), skipVTables); @@ -16680,6 +16681,8 @@ namespace CppSharp __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; *((global::CppSharp.Parser.AST.LabelStmt.__Internal*) __Instance) = *((global::CppSharp.Parser.AST.LabelStmt.__Internal*) _0.__Instance); + if (_0.__name_OwnsNativeMemory) + this.Name = _0.Name; } public global::CppSharp.Parser.SourceLocation IdentLoc @@ -16718,6 +16721,9 @@ namespace CppSharp set { + if (__name_OwnsNativeMemory) + Marshal.FreeHGlobal(((__Internal*)__Instance)->name); + __name_OwnsNativeMemory = true; if (value == null) { ((__Internal*)__Instance)->name = global::System.IntPtr.Zero; @@ -25181,6 +25187,7 @@ namespace CppSharp internal static extern __IntPtr cctor(__IntPtr __instance, __IntPtr _0); } + private bool __castKindName_OwnsNativeMemory = false; internal static new CastExpr __CreateInstance(__IntPtr native, bool skipVTables = false) { return new CastExpr(native.ToPointer(), skipVTables); @@ -25247,6 +25254,8 @@ namespace CppSharp __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; *((global::CppSharp.Parser.AST.CastExpr.__Internal*) __Instance) = *((global::CppSharp.Parser.AST.CastExpr.__Internal*) _0.__Instance); + if (_0.__castKindName_OwnsNativeMemory) + this.CastKindName = _0.CastKindName; } public static implicit operator global::CppSharp.Parser.AST.CastExpr(global::CppSharp.Parser.AST.StmtClass klass) @@ -25290,6 +25299,9 @@ namespace CppSharp set { + if (__castKindName_OwnsNativeMemory) + Marshal.FreeHGlobal(((__Internal*)__Instance)->castKindName); + __castKindName_OwnsNativeMemory = true; if (value == null) { ((__Internal*)__Instance)->castKindName = global::System.IntPtr.Zero; @@ -30986,6 +30998,7 @@ namespace CppSharp internal static extern __IntPtr cctor(__IntPtr __instance, __IntPtr _0); } + private bool __castName_OwnsNativeMemory = false; internal static new CXXNamedCastExpr __CreateInstance(__IntPtr native, bool skipVTables = false) { return new CXXNamedCastExpr(native.ToPointer(), skipVTables); @@ -31052,6 +31065,8 @@ namespace CppSharp __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; *((global::CppSharp.Parser.AST.CXXNamedCastExpr.__Internal*) __Instance) = *((global::CppSharp.Parser.AST.CXXNamedCastExpr.__Internal*) _0.__Instance); + if (_0.__castName_OwnsNativeMemory) + this.CastName = _0.CastName; } public static implicit operator global::CppSharp.Parser.AST.CXXNamedCastExpr(global::CppSharp.Parser.AST.StmtClass klass) @@ -31068,6 +31083,9 @@ namespace CppSharp set { + if (__castName_OwnsNativeMemory) + Marshal.FreeHGlobal(((__Internal*)__Instance)->castName); + __castName_OwnsNativeMemory = true; if (value == null) { ((__Internal*)__Instance)->castName = global::System.IntPtr.Zero; diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 891e58f4..45d540f5 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -455,16 +455,16 @@ namespace CppSharp.Generators.CSharp var dict = $@"global::System.Collections.Concurrent.ConcurrentDictionary"; WriteLine("internal static readonly {0} NativeToManagedMap = new {0}();", dict); - - // Add booleans to track who owns unmanaged memory for string fields - foreach (var field in @class.Layout.Fields.Where(f => f.QualifiedType.Type.IsConstCharString())) - { - WriteLine($"private bool __{field.Name}_OwnsNativeMemory = false;"); - } } PopBlock(NewLineKind.BeforeNextBlock); } + // Add booleans to track who owns unmanaged memory for string fields + foreach (var prop in @class.GetConstCharFieldProperties()) + { + WriteLine($"private bool __{prop.Field.OriginalName}_OwnsNativeMemory = false;"); + } + GenerateClassConstructors(@class); GenerateClassMethods(@class.Methods); @@ -2302,7 +2302,7 @@ namespace CppSharp.Generators.CSharp // } // // IDisposable.Dispose() and Object.Finalize() set callNativeDtor = Helpers.OwnsNativeInstanceIdentifier - WriteLine($"if (callNativeDtor)"); + WriteLine("if (callNativeDtor)"); if (@class.IsDependent || dtor.IsVirtual) WriteOpenBraceAndIndent(); else @@ -2325,10 +2325,11 @@ namespace CppSharp.Generators.CSharp // unmanaged memory isn't always initialized and/or a reference may be owned by the // native side. // - foreach (var field in @class.Layout.Fields.Where(f => f.QualifiedType.Type.IsConstCharString())) + foreach (var prop in @class.GetConstCharFieldProperties()) { - var ptr = $"(({Helpers.InternalStruct}*){Helpers.InstanceIdentifier})->{field.Name}"; - WriteLine($"if (__{field.Name}_OwnsNativeMemory)"); + string name = prop.Field.OriginalName; + var ptr = $"(({Helpers.InternalStruct}*){Helpers.InstanceIdentifier})->{name}"; + WriteLine($"if (__{name}_OwnsNativeMemory)"); WriteLineIndent($"Marshal.FreeHGlobal({ptr});"); } @@ -2978,17 +2979,13 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty // Copy any string references owned by the source to the new instance so we // don't have to ref count them. - foreach (var field in @class.Fields.Where(f => f.QualifiedType.Type.IsConstCharString())) + // If there is no property or no setter then this instance can never own the native + // memory. Worry about the case where there's only a setter (write-only) when we + // understand the use case and how it can occur. + foreach (var prop in @class.GetConstCharFieldProperties()) { - var prop = @class.Properties.Where(p => p.Field == field).FirstOrDefault(); - // If there is no property or no setter then this instance can never own the native - // memory. Worry about the case where there's only a setter (write-only) when we - // understand the use case and how it can occur. - if (prop != null && prop.HasGetter && prop.HasSetter) - { - WriteLine($"if ({method.Parameters[0].Name}.__{field.OriginalName}_OwnsNativeMemory)"); - WriteLineIndent($@"this.{prop.Name} = {method.Parameters[0].Name}.{prop.Name};"); - } + WriteLine($"if ({method.Parameters[0].Name}.__{prop.Field.OriginalName}_OwnsNativeMemory)"); + WriteLineIndent($"this.{prop.Name} = {method.Parameters[0].Name}.{prop.Name};"); } } } @@ -3005,7 +3002,7 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty } public void GenerateInternalFunctionCall(Function function, - QualifiedType returnType = default(QualifiedType)) + QualifiedType returnType = default) { var @class = function.Namespace as Class;