[Haskell-cafe] threepenny-gui: garbage collection
Tom Ellis
tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk
Mon Jan 5 17:37:08 UTC 2015
On Mon, Jan 05, 2015 at 06:31:57PM +0100, Heinrich Apfelmus wrote:
> 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.
> >
> > http://stackoverflow.com/questions/4526829/why-and-how-to-avoid-event-handler-memory-leaks
>
> 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.)
I am suggesting that the `Event` originates elsewhere, perhaps from some
other widget that we wish to have a long life, but we want to attach
short-lived widgets to it via event handlers. Can we do that without having
to explicitly unregister the handlers when we want the short-lived widgets
to be garbage collected?
Tom
More information about the Haskell-Cafe
mailing list