Note that there are some explanatory texts on larger screens.

plurals
  1. POLambdas within Extension methods: Possible memory leak?
    primarykey
    data
    text
    <p>I just gave an answer to a <a href="https://stackoverflow.com/questions/2583565/delay-ring-between-two-font-style-changing/2583713#2583713">quite simple question</a> by using an extension method. But after writing it down i remembered that you can't unsubscribe a lambda from an event handler.</p> <p>So far no big problem. But how does all this behave within an extension method??</p> <p>Below is my code snipped again. So can anyone enlighten me, if this will lead to myriads of timers hanging around in memory if you call this extension method multiple times?</p> <p>I would say no, cause the scope of the timer is limited within this function. So after leaving it no one else has a reference to this object. I'm just a little unsure, cause we're here within a static function in a static class.</p> <pre><code>public static class LabelExtensions { public static Label BlinkText(this Label label, int duration) { System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); timer.Interval = duration; timer.Tick += (sender, e) =&gt; { timer.Stop(); label.Font = new Font(label.Font, label.Font.Style ^ FontStyle.Bold); }; label.Font = new Font(label.Font, label.Font.Style | FontStyle.Bold); timer.Start(); return label; } } </code></pre> <h3>Update</h3> <p>Just to clarify i used <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx" rel="nofollow noreferrer">System.Windows.Forms.Timer</a>. So from your answers it seems, that especially using this timer class just was the right choice cause it does anything the way as i would expect it in this case. If you try another timer class in this case you can run into trouble as <a href="https://stackoverflow.com/questions/2584009/lambdas-within-extension-methods-possible-memory-leak/2584218#2584218">Matthew found out</a>. Also i <a href="https://stackoverflow.com/questions/2584009/lambdas-within-extension-methods-possible-memory-leak/2584457#2584457">found a way</a> by using <a href="http://msdn.microsoft.com/en-us/library/system.weakreference.aspx" rel="nofollow noreferrer">WeakReference</a>s if my objects are staying alive or not.</p> <h3>Update 2</h3> <p>After a little sleep and a little more thinking, i also made another change to my tester (<a href="https://stackoverflow.com/questions/2584009/lambdas-within-extension-methods-possible-memory-leak/2584457#2584457">answer below</a>) i just added a <code>GC.Collect()</code> after the last line and set the duration to 10000. After starting the <code>BlinkText()</code> several times i hitted all the time my button2 to get the current state and to force a garbage collection. And as it seems all the timers will be destroyed <strong>after</strong> calling the <code>Stop()</code> method. So also a garbage collection while my BlinkText is already left and the timer is running doesn't lead to any problems.</p> <p>So after all your good responses and a little more testing i can happily say that it just does what it should without leaving timers in the memory nor throwing away the timers before they done their work.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload