[Haskell-cafe] threepenny-gui: garbage collection
apfelmus at quantentunnel.de
Sat Jan 3 10:34:07 UTC 2015
Tom Ellis wrote:
> Suppose that each time I make a particular threepenny-gui widget, call it a
> `Foo`, I register an event handler using `on` so that the `Foo` can change
> its appearance based on some `Event`.
> Suppose I create, show, and hide a billion `Foo`s, each one never to be
> shown again. Can they be garbage collected? Presumably there is something
> storing the callback that was registered for the `Event` and the callback
> refers to the `Foo`. How then can the `Foo`s be freed?
> `Reactive.Threepenny.register` talks about an "unregister" action but says
> "FIXME: Unregistering event handlers does not work yet.". But how could it
> be known anyway when to unregister? Is there not the possibility of a space
> leak regardless?
(Threepenny author here.)
This is a classic example of a circular reference that occurs in every
GUI program: A widget keeps a reference to an event handler, which in
turn keeps a reference to the widget.
Of course, GHC's has a well-designed garbage collector that can handle
this, but in Threepenny, the situation is actually worse: the widget is
by the Haskell runtime. I don't know any garbage collector that can
resolve cross-runtime circular dependencies.
Still, I managed to solve this problem to some extend. :) Essentially,
Haskell side, and thus allows the Haskell garbage collector to handle
ten-foot pole.) For your concrete situation, this means that as long as
the `Foo` widgets are not inserted into the DOM tree in the browser, and
you only use DOM events on them (`UI.click` etc), they will indeed be
garbage collected properly whenever their lifetime ends in your Haskell
program. (At least, that's the plan. I have tested that it works, but
garbage collection is a tricky business, so I wouldn't bet my life on it
works for the `Element` type at the moment. If you use the `ffiExport`
not be garbage collected.
The `Reactive.Threepenny.register` function is totally separate from
right that there might be some issues with `unregister`, but I haven't
thought about this yet.
More information about the Haskell-Cafe