[Haskell-cafe] threepenny-gui: garbage collection
apfelmus at quantentunnel.de
Mon Jan 5 17:31:57 UTC 2015
Tom Ellis wrote:
> Heinrich Apfelmus wrote:
>> 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?
>> 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.
> Hi Heinrich, thanks for your reply.
> Actually I don't think I am thinking of a circular reference problem. The
> problem I am envisaging is that the `Event` keeps a reference to the event
> handler which in turn keeps a reference to the widget. Thus it is hard to
> see how the widget can be freed before the `Event`. A quick Google shows
> this indeed a common problem in GUI programming, e.g.
Consider this: Who keeps a reference to the `Event`?
(If no one keeps a reference to the `Event`, then it can be garbage
collected, and so can the event handler and the widget.)
> Is my understanding correct, or is my model of how `Event`s work in
> threepenny-gui wrong?
>> Of course, GHC's has a well-designed garbage collector that can
>> handle this, but in Threepenny, the situation is actually worse: the
>> handler is managed by the Haskell runtime. I don't know any garbage
>> collector that can resolve cross-runtime circular dependencies.
> I haven't managed to understand the issue here. Could you say a bit more
> about that? It sounds interesting! I don't think it's related to my
> concern though, but if it is that would also be interesting.
The issue is still that there is a circular dependency
widget -> Event -> event handler -> widget
Now, this wouldn't be a problem if all these references were managed by
GHC. Our beloved Haskell compiler can handle circular references just
fine, they will be garbage collected at once whenever there is no more
"outside" reference to the whole cycle.
The real issue, which is specific to Threepenny, is that not all
references are managed by GHC. Instead, some are managed by the
dependency crosses language boundaries -- and now it becomes hard to
garbage collect it.
More information about the Haskell-Cafe