When passing the callback delegate to C from C# during static init,
it appears (but only on Windows) this actually creates a C# class
instance for the delegate and passes that. However, that class
instance is eventually GC'd and our callbacks will start failing.
Instead, we create a static instance of of the delegate and pass
that to C.
}}
}}
public delegate {jret} {fn_suffix}_callback(int obj_ptr, int fn_id{jargs});
+ static {fn_suffix}_callback {fn_suffix}_callback_inst = c_callback_{fn_suffix};
""")
bindings.write(self.native_meth_decl(f"register_{fn_suffix}_invoker", "int") + f"({fn_suffix}_callback callee);\n")
# Easiest way to get a static run is just define a variable, even if we dont care
- bindings.write(f"\tstatic int _run_{fn_suffix}_registration = register_{fn_suffix}_invoker(c_callback_{fn_suffix});")
+ bindings.write(f"\tstatic int _run_{fn_suffix}_registration = register_{fn_suffix}_invoker({fn_suffix}_callback_inst);")
bindings.write("""
}