gotk4
                                
                                 gotk4 copied to clipboard
                                
                                    gotk4 copied to clipboard
                            
                            
                            
                        Refactor SetFinalizer and toggle notifier
Preamble
Ideally, closures should not hold any kind of reference to GObjects. C code doesn't do this: there's no capturing, and closures all take and release references as they're called instead of holding a reference for as long as the object is alive.
Currently, Go's finalizer fails us. Ou closures reference objects throughout the duration of the program, causing a finalizer cycle. This Can we make the finalizer do less and do our work in the toggle notifier?
Proposal
- Go should hold a strong or weak reference to the object; the finalizer unrefs it
- We'll handle freeing the object using a C-based toggle notifier
Can we get away with using WeakRefs for the Go object? Is there a way to combine both that and the Go finalizer to detect when to actually free?
Alternative
An interesting alternative would be to make objects floating/weak by default. Users who want to revive a GObject later on must manually sink the reference with .Sink() or be extra careful with how they take the object away from C memory.
If we do this, how would subclassed objects work? We would have to keep a Go reference. This might overcomplicate things.
Alternative
Have a WeakBox structure:
type SignalBox coreglib.SignalBox[Main]
signalBox := SignalBox{main}
s.ConnectOpen(func(signalBox *SignalBox) {
  // use signalBox
}, signalBox)
This is guaranteed to work, but it sucks.