From a3a765470f08d77c7b05f9262cd0f936e04a1e94 Mon Sep 17 00:00:00 2001
From: Jens Wallauer <wallauer@rhd-instruments.de>
Date: Thu, 11 Jul 2024 15:32:01 +0200
Subject: [PATCH] Fixed potential CallbackOnCollectedDelegate exception.

---
 src/network.cs | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/network.cs b/src/network.cs
index 4e64a9b..3eeebc3 100644
--- a/src/network.cs
+++ b/src/network.cs
@@ -11,6 +11,8 @@ namespace Kiss
 /// </summary>
 public class Network: System.IDisposable
 {
+	private readonly Kiss.Capi.resultDlg resultCallback;
+
 	public delegate void ResultDlg(float[] result, Network network);
 
 	private struct Flight
@@ -52,8 +54,12 @@ public class Network: System.IDisposable
 	/// </exception>
 	public Network(string path, bool verbose = false)
 	{
+		// Keep the callback in a class variable to prevent the garbage collector from collecting it
+		// which would cause a CallbackOnCollectedDelegate exception.
+		this.resultCallback = this.CResultCb;
+
 		Utils.CheckEnvThrow();
-		byte ret = Capi.kiss_load_network_prealloc(ref net, Capi.StringToPlatformBytes(path), CResultCb, Convert.ToByte(verbose));
+		byte ret = Capi.kiss_load_network_prealloc(ref net, Capi.StringToPlatformBytes(path), this.resultCallback, Convert.ToByte(verbose));
 		Console.WriteLine("ret: {0:D}", ret);
 		if(ret == 0)
 			throw new System.IO.FileLoadException(getError());
-- 
GitLab