diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index e61ab045..6c48b367 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -1867,7 +1867,8 @@ namespace CppSharp.Generators.CSharp } } - WriteLine("Marshal.FreeHGlobal({0});", Helpers.InstanceIdentifier); + WriteLine("if ({0})", Helpers.OwnsNativeInstanceIdentifier); + WriteLineIndent("Marshal.FreeHGlobal({0});", Helpers.InstanceIdentifier); WriteCloseBraceIndent(); PopBlock(NewLineKind.BeforeNextBlock);