diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 0ad09b42..f9853a67 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -401,9 +401,8 @@ namespace CppSharp.Generators.CSharp public bool VisitDelegateType(FunctionType function, string type) { - Context.Return.Write("Marshal.GetFunctionPointerForDelegate({0})", - Context.Parameter.Name); - + Context.Return.Write("{0} == null ? global::System.IntPtr.Zero : Marshal.GetFunctionPointerForDelegate({0})", + Context.Parameter.Name); return true; } diff --git a/tests/CSharpTemp/CSharpTemp.Tests.cs b/tests/CSharpTemp/CSharpTemp.Tests.cs index 2f3f87cf..2ac01f5c 100644 --- a/tests/CSharpTemp/CSharpTemp.Tests.cs +++ b/tests/CSharpTemp/CSharpTemp.Tests.cs @@ -327,4 +327,14 @@ public class CSharpTempTests : GeneratorTestFixture dervClass = new TestParamToInterfacePass(dervClass + baseInterface); Assert.AreEqual(dervClass.M, 2); } + + [Test] + public void TestNullAttributedFunctionPtr() + { + using (var foo = new Foo()) + { + foo.AttributedFunctionPtr = null; + } + } + } \ No newline at end of file