OK, I've tried some more things, and then realized that what I was asking is probably something weird
I thought about how other languages solve this problem, and I just realized that unregistering callbacks is always a bit awkward
I thought this could be handled with the lifespan of the function handle, but this is not usually the case in other languages. So I reckon this might not be the best way to go, and is perhaps not always what you want, as it requires the user to keep the thing alive/in scope some how.
You did help me a great deal in understanding what is going on, and have given me a couple of options to solve this. At least now I know how to let the App know when the delegate CANNOT be called (and the user forgot to unregister the callback). I particularly like the callback function option, as it eliminates inactive objects that stay there simply because the didn't get called.
The Object weakref is nice to make sure I don't take ownership of the object.
I think I will go with the C# way of deregistering callbacks, by passing the delegate once for register, and another time to unregister it.
So perhaps something like:
timer.register(timerCallback(myObject.myFunction)); timer.unregister(timerCallback(myObject.myFunction)); //Or perhaps I can even go full C# and make this work :) timer.event += timerCallback(myObject.myFunction); timer.event -= timerCallback(myObject.myFunction);
I noticed that when a function handle is passed it is the same, even when stored in different handle instances, and the delegates are not, but I can use GetDelegateFunction in that case. I'll use the function pointers to identify which callback to remove, and use the userdata callback to clean up callbacks that are no longer valid.
Thank you very much for the information, and taking the time of helping me with this.