gabe...@gmail.com
unread,Apr 20, 2015, 7:09:13 PM4/20/15Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to bac...@googlegroups.com
One of the tricky parts of using event listeners is the avoidance of leaks -- ie, how to clean up unneeded references without having to manually unhook each listener. I propose a simple implementation, and I'd like to ask why Bacon doesn't do it this way, and to get a sense of the pitfalls of this approach (if any).
The idea is best illustrated with an example. Suppose you have three nodes (A, B, and C) connected in the following manner, with the first node (A) referenced by some closure scope:
1. scope -> A ---> B ---> C -> logger
Here, the forward arrow (--->) indicates the direction of data flow.
Now suppose that we are no longer in interested in using C. So we unhook the logger. Now the picture is as follows:
2. scope -> A ---> B ---> C
But because a reference is held to A in scope, and A references B, and B references C, neither B nor C will be garbage collected.
My proposal is to keep back pointers, so the initial situation (1) would instead look like this:
3. scope -> A <--> B <--> C -> logger
After you unhook the logger, we walk backwards, unhooking the forward pointer, so we end up with this:
4. scope -> A <--- B <--- C
Now, the cool part is that, if I no longer hold a reference to C, the garbage collector can eventually kill B and C, resulting in this:
5. scope -> A
On the other hand, if I did hold a reference to C, and wanted to do something with it in the future, such as reconnect the logger, the underlying implementation could walk the back pointers and "reconnect" the event stream, resulting in the same topology as in (3).
Thoughts?