<div dir="ltr">Which FRP frameworks have you been looking at?<div style><br>In my experience, the most publicized leaks have been time leaks, which are a particular type of memory leak related to switching.  However, the presence of time leaks mostly arises in terms of the FRP implementation.  Arrowized FRP (e.g. Yampa, netwire) do not typically suffer from this for example.  Some libraries that implement the semantics of Conal Elliott&#39;s &quot;Push-pull functional reactive programming&quot; (or similar semantics) have been susceptible to this, however recent implementations are not.  Sodium, elerea, and reactive-banana for example have generally overcome the worst issues present in earlier systems.  Leaks can still be present in current systems of course, but now they&#39;re generally due to the programmer unintentionally retaining data in a case that&#39;s much simpler to reason about.  That is, the situation today is more similar to forgetting to use deepseq or similar, rather than the prior leaks that were very difficult to reason about.</div>
<div style><br></div><div style>I think the most common current issue is that a very natural way of accumulating reactive events across time can leak.  Suppose you have a library of reactive widgets, where each widget has an associated stream of IO actions that you want to run.  E.g. clicking a button prints it, sliding a scale prints the value, etc.</div>
<div style><br></div><div style>&gt; class Actionable a where</div><div style>&gt;   actions :: a -&gt; Event (IO ())</div><div style><br></div><div style>suppose you have a collection that allows you to add/remove Actionable things to it (e.g. a button panel).  This panel has an action stream that&#39;s simply the concatenation of those of its components.  One possible implementation looks like this:</div>
<div style><br></div><div style>&gt; data ButtonPanel = ButtonPanel (Event (IO ())</div><div style><br></div><div style>&gt; emptyPanel = ButtonPanel mempty</div><div style><br></div><div style>&gt; addActionable :: Actionable a =&gt; ButtonPanel -&gt; a -&gt; ButtonPanel</div>
<div style>&gt; addActionable (ButtonPanel e) a = ButtonPanel (e &lt;&gt; actions a)</div><div style><br></div><div style>I&#39;ve omitted all the parts for wiring up the gui, but suppose they&#39;re handled also, and removing a button from the panel just removes it from the gui and destroys the widget.  After that, the button&#39;s event stream is empty, so you can just leave the ButtonPanel&#39;s event stream unchanged, because the destroyed button will never fire.</div>
<div style><br></div><div style>This is a memory leak.  The destroyed Button&#39;s event stream is still referenced in the ButtonPanel event stream, so data related to it never gets freed.  Over time your FRP network will grow, and eventually you&#39;ll hit scaling problems.</div>
<div style><br></div><div style>The proper solution in this instance is to keep a list of each button&#39;s event stream within the button panel.  It&#39;s ok to keep a cached aggregate stream, but that cache needs to be re-built when a button is removed.  This is usually fairly natural to do with FRP, but your ButtonPanel may look like this instead:</div>
<div style><br></div><div style>&gt; data ButtonPanel = ButtonPanel  (Map Key (Event (IO ()))</div><div style><br></div><div style>&gt; addActionable :: Actionable a =&gt; ButtonPanel-&gt; Key -&gt; a -&gt; ButtonPanel</div>
<div style>&gt; removeActionable :: ButtonPanel -&gt; Key -&gt; ButtonPanel</div><div style><br></div><div style>and now you need to manage some sort of Key for collection elements.</div><div style><br></div><div style>This style isn&#39;t entirely idiomatic FRP.  Instead of these functions, you could have all your modifications handled via the FRP framework.  For example,</div>
<div style><br></div><div style>&gt; data ButtonPanel = ButtonPanel (Behavior (Map Key (Event (IO ()))))</div><div style>&gt; buttonPanel :: Actionable a =&gt; Event (Key,a) -&gt; Event Key -&gt; ButtonPanel<br></div><div style>
<br></div><div style>but you still need to be aware that objects can reference older objects.  Behaviors are frequently created via accumulators over events (e.g. accumB), and if the accumulation is doing something like &#39;mappend&#39;, a memory leak is likely.</div>
<div style><br></div><div style>Basically, the issue is that when you&#39;re accumulating reactive stuff over time, you need to be sure that your accumulator doesn&#39;t reference data that is otherwise expired.  This example uses a push-pull style pseudocode because that&#39;s what I&#39;m most familiar with.  I&#39;m not entirely show how (or if) this translates to arrowized FRP, although it wouldn&#39;t surprise me if there&#39;s a similar pattern.</div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Jun 6, 2013 at 2:50 AM, Łukasz Dąbek <span dir="ltr">&lt;<a href="mailto:sznurek@gmail.com" target="_blank">sznurek@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div>Hello, Cafe!<br><br></div>I&#39;ve heard that one of the problems of FRP (Functional Reactive Programming) is that it&#39;s easy to create memory leaks. However I cannot find any natural examples of such leaks. Could anybody post some (pseudo)code demonstrating this phenomenon? Preferably something that arises when one is writing bigger applications in FRP style.<br>


<br></div>Thanks in advance!<br><br>--<br></div>Łukasz Dąbek<br></div>
<br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br></div>