Add check to prevent double-free from native event in C#

This commit is contained in:
Wojtek Figat
2021-08-11 19:28:53 +02:00
parent 622951c6be
commit d65a85e30c

View File

@@ -665,7 +665,11 @@ namespace Flax.Build.Bindings
indent += " ";
var eventInstance = eventInfo.IsStatic ? string.Empty : "__unmanagedPtr, ";
contents.Append(indent).Append($"add {{ Internal_{eventInfo.Name} += value; if (Internal_{eventInfo.Name}_Count++ == 0) Internal_{eventInfo.Name}_Bind({eventInstance}true); }}").AppendLine();
contents.Append(indent).Append($"remove {{ Internal_{eventInfo.Name} -= value; if (--Internal_{eventInfo.Name}_Count == 0) Internal_{eventInfo.Name}_Bind({eventInstance}false); }}").AppendLine();
contents.Append(indent).Append("remove { ").AppendLine();
contents.Append("#if FLAX_EDITOR || BUILD_DEBUG").AppendLine();
contents.Append(indent).Append($"if (Internal_{eventInfo.Name} != null) {{ bool invalid = true; foreach (Delegate e in Internal_{eventInfo.Name}.GetInvocationList()) {{ if (e == (Delegate)value) {{ invalid = false; break; }} }} if (invalid) throw new Exception(\"Cannot unregister from event if not registered before.\"); }}").AppendLine();
contents.Append("#endif").AppendLine();
contents.Append(indent).Append($"Internal_{eventInfo.Name} -= value; if (--Internal_{eventInfo.Name}_Count == 0) Internal_{eventInfo.Name}_Bind({eventInstance}false); }}").AppendLine();
indent = indent.Substring(0, indent.Length - 4);
contents.Append(indent).Append('}').AppendLine();