gotk4 icon indicating copy to clipboard operation
gotk4 copied to clipboard

Refactor SetFinalizer and toggle notifier

Open diamondburned opened this issue 2 years ago • 2 comments

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?

diamondburned avatar Feb 15 '23 21:02 diamondburned

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.

diamondburned avatar Feb 15 '23 21:02 diamondburned

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.

diamondburned avatar Mar 03 '23 00:03 diamondburned