From 201e9467410ea331c1a40bbd7457aef1acedbe34 Mon Sep 17 00:00:00 2001 From: Zae Date: Thu, 25 Jan 2024 02:40:08 +0800 Subject: [PATCH] C#: Fix incorrect GC handle for non-instantiable types. --- modules/mono/editor/bindings_generator.cpp | 10 +++++++--- .../GodotSharp/GodotSharp/Core/GodotObject.base.cs | 2 ++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/modules/mono/editor/bindings_generator.cpp b/modules/mono/editor/bindings_generator.cpp index 25a5720bc48..41d05dfdd00 100644 --- a/modules/mono/editor/bindings_generator.cpp +++ b/modules/mono/editor/bindings_generator.cpp @@ -1621,9 +1621,13 @@ Error BindingsGenerator::_generate_cs_type(const TypeInterface &itype, const Str << CLOSE_BLOCK_L2 CLOSE_BLOCK_L1; } else { // Hide the constructor - output.append(MEMBER_BEGIN "internal "); - output.append(itype.proxy_name); - output.append("() {}\n"); + output << MEMBER_BEGIN "internal " << itype.proxy_name << "() : this(" + << (itype.memory_own ? "true" : "false") << ")\n" OPEN_BLOCK_L1 + << INDENT2 "unsafe\n" INDENT2 OPEN_BLOCK + << INDENT3 "_ConstructAndInitialize(null, " + << BINDINGS_NATIVE_NAME_FIELD ", CachedType, refCounted: " + << (itype.is_ref_counted ? "true" : "false") << ");\n" + << CLOSE_BLOCK_L2 CLOSE_BLOCK_L1; } // Add.. em.. trick constructor. Sort of. diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotObject.base.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotObject.base.cs index 8f8e884b8cc..84b2a04276d 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotObject.base.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotObject.base.cs @@ -38,6 +38,8 @@ namespace Godot { if (NativePtr == IntPtr.Zero) { + Debug.Assert(nativeCtor != null); + NativePtr = nativeCtor(); InteropUtils.TieManagedToUnmanaged(this, NativePtr,