{"id":58,"date":"2011-03-11T13:09:00","date_gmt":"2011-03-11T13:09:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2011\/03\/11\/did-i-say-ive-got-memory-leak\/"},"modified":"2011-03-11T13:09:00","modified_gmt":"2011-03-11T13:09:00","slug":"did-i-say-ive-got-memory-leak","status":"publish","type":"post","link":"http:\/\/panahy.nl\/index.php\/2011\/03\/11\/did-i-say-ive-got-memory-leak\/","title":{"rendered":"Did i say &#8220;I&#8217;ve got memory leak&#8221;?"},"content":{"rendered":"<p>Dealing with <b>memory leak<\/b> is not as far as some might think it is. But it doesn&#8217;t get serious until it is very late to change the fundamentals of our software design. <\/p>\n<div><\/div>\n<div>In my recent project it happened to be caused by <i>traditional event subscription pattern<\/i>. Which is most likely not leaving any memory leak behind, but it could easily do so \ud83d\ude41<\/div>\n<div><\/div>\n<div>I also looked into latest libraries of Microsoft and saw they are using a new way of subscribing event handler  to GUI events in WPF.  Further I saw that every Window, like any Visual object in WPF, is derrived from <b><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.threading.dispatcherobject.aspx\">DispatcherObject <\/a><\/b>and is also implementing <b><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.iweakeventlistener.aspx\">IWeakEventListener<\/a><\/b>; hummmmmm!<\/div>\n<div><\/div>\n<div>So I&#8217;ve decided to create my first <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.weakeventmanager.aspx\">WeakEventManager <\/a>and followed the instructions by <a href=\"http:\/\/reedcopsey.com\/2009\/08\/06\/preventing-event-based-memory-leaks-weakeventmanager\/\">Reed Copsey Jr<\/a>. in his blog<\/div>\n<div><\/div>\n<div>You can instanciate several instances of different event listeners and let the manager route the events to all of them. As long as they all implement IWeakEventListener.ReceiveWeakEvent<\/div>\n<p><\/p>\n<div>To demonstrate this I started a sample code about human body. Assume you have a heart class and a hand class. If you have an instance of them and you need to get events on your hand when the heart beats you might do something like:<br \/><code>hear.BeatEvent += right.BeatHandler<\/code><br \/>The idea is to change this aby using an event manager, in my sample I have called it Brain, to handle de notifications like <br \/><code>Brain.AddListener(heart, right);<\/code><br \/>And to unsubscribe to the event you do something like<br \/><code>Brain.RemoveListener(heart, right);<\/code><\/p>\n<p>To make this possible, you need to make sure that your Hand class where the events need to be handled, you implement the IWeakEventListener interface which brings us to ReceiveWeakEvent method:<\/p>\n<pre><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;summary&gt;<\/span><span style=\"color: #008000;\"> Receives events from the centralized event manager. <\/span><span style=\"color: #808080;\">&lt;\/summary&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;returns&gt;<\/span><span style=\"color: #008000;\"> true if the listener handled the event.  <\/span><span style=\"color: #808080;\">&lt;\/returns&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;param name=\"managerType\"&gt;<\/span><span style=\"color: #008000;\">The type of the <\/span><span style=\"color: #808080;\">&lt;see cref=\"T:System.Windows.WeakEventManager\"\/&gt;<\/span><span style=\"color: #008000;\"> calling this method.<\/span><span style=\"color: #808080;\">&lt;\/param&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;param name=\"sender\"&gt;<\/span><span style=\"color: #008000;\">Object that originated the event.<\/span><span style=\"color: #808080;\">&lt;\/param&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;param name=\"e\"&gt;<\/span><span style=\"color: #008000;\">Event data.<\/span><span style=\"color: #808080;\">&lt;\/param&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #0000FF;\">public<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">bool<\/span><span style=\"color: #000000;\"> ReceiveWeakEvent(Type managerType, <\/span><span style=\"color: #0000FF;\">object<\/span><span style=\"color: #000000;\"> sender, EventArgs e)<br \/>{<br \/>    <\/span><span style=\"color: #0000FF;\">if<\/span><span style=\"color: #000000;\"> (managerType == <\/span><span style=\"color: #0000FF;\">typeof<\/span><span style=\"color: #000000;\">(Brain))<br \/>    {<br \/>        var beat = e <\/span><span style=\"color: #0000FF;\">as<\/span><span style=\"color: #000000;\"> BeatArgs;<br \/>        Trace.WriteLine(<\/span><span style=\"color: #0000FF;\">string<\/span><span style=\"color: #000000;\">.Format(<\/span><span style=\"color: #800000;\">\"{0} hand is getting the beat from {1}\"<\/span><span style=\"color: #000000;\">, name, beat.TimeOfBeat.ToString(<\/span><span style=\"color: #800000;\">\"hh:mm:ss\"<\/span><span style=\"color: #000000;\">)));<br \/>        <\/span><span style=\"color: #0000FF;\">return<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">true<\/span><span style=\"color: #000000;\">;<br \/>    }<br \/><br \/>    <\/span><span style=\"color: #0000FF;\">return<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">false<\/span><span style=\"color: #000000;\">;<br \/>}<\/span><\/pre>\n<p>The Brain class which has a Singleton implementation provides only two public methods i.e. <code>AddListener<\/code> and <code>RemoveListener<\/code>. They do this by calling the protected methods and passing the parameters:<\/p>\n<pre><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;summary&gt;<\/span><span style=\"color: #008000;\">Subscribe a body part to listen to heart beat <\/span><span style=\"color: #808080;\">&lt;\/summary&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;param name=\"source\"&gt;<\/span><span style=\"color: #008000;\">the heart to listen to<\/span><span style=\"color: #808080;\">&lt;\/param&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;param name=\"listener\"&gt;<\/span><span style=\"color: #008000;\">the body that subscribes<\/span><span style=\"color: #808080;\">&lt;\/param&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #0000FF;\">public<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">static<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">void<\/span><span style=\"color: #000000;\"> AddListener(Heart source, IWeakEventListener listener)<br \/>{<br \/>    Neuron.ProtectedAddListener(source, listener);<br \/>}<br \/><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;summary&gt;<\/span><span style=\"color: #008000;\">Unsubscribe a body part to listen to heart beat <\/span><span style=\"color: #808080;\">&lt;\/summary&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;param name=\"source\"&gt;<\/span><span style=\"color: #008000;\">the heart to listen to<\/span><span style=\"color: #808080;\">&lt;\/param&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;param name=\"listener\"&gt;<\/span><span style=\"color: #008000;\">the body that subscribes<\/span><span style=\"color: #808080;\">&lt;\/param&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #0000FF;\">public<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">static<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">void<\/span><span style=\"color: #000000;\"> RemoveListener(Heart source, IWeakEventListener listener)<br \/>{<br \/>    Neuron.ProtectedRemoveListener(source, listener);<br \/>}<\/span><\/pre>\n<p>That is not all what brain do internally. It is actually inheriting the abstract class <code>WeakEventManager<\/code> and in order to do that it overrides the <code>StartListening <\/code>and <code>StopListening<\/code> methods that are internally called when it should be.<\/p>\n<pre><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;summary&gt;<\/span><span style=\"color: #008000;\">Starts listening on the provided heart for the managed beat event.<\/span><span style=\"color: #808080;\">&lt;\/summary&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;param name=\"source\"&gt;<\/span><span style=\"color: #008000;\">The heart to disconnect beat event<\/span><span style=\"color: #808080;\">&lt;\/param&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #0000FF;\">protected<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">override<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">void<\/span><span style=\"color: #000000;\"> StartListening(<\/span><span style=\"color: #0000FF;\">object<\/span><span style=\"color: #000000;\"> source)<br \/>{<br \/>    var heart = source <\/span><span style=\"color: #0000FF;\">as<\/span><span style=\"color: #000000;\"> Heart;<br \/>    <\/span><span style=\"color: #0000FF;\">if<\/span><span style=\"color: #000000;\"> (heart == <\/span><span style=\"color: #0000FF;\">null<\/span><span style=\"color: #000000;\">) <\/span><span style=\"color: #0000FF;\">throw<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">new<\/span><span style=\"color: #000000;\"> ArgumentException(<\/span><span style=\"color: #800000;\">\"The brain suppose to listen to heart only\"<\/span><span style=\"color: #000000;\">);<br \/><br \/>    heart.BeatEventHandler += DeliverEvent;<br \/>}<br \/><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;summary&gt;<\/span><span style=\"color: #008000;\">Stops listening on the provided heart for the managed beat event.<\/span><span style=\"color: #808080;\">&lt;\/summary&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;param name=\"source\"&gt;<\/span><span style=\"color: #008000;\">The heart to disconnect beat event<\/span><span style=\"color: #808080;\">&lt;\/param&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #0000FF;\">protected<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">override<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">void<\/span><span style=\"color: #000000;\"> StopListening(<\/span><span style=\"color: #0000FF;\">object<\/span><span style=\"color: #000000;\"> source)<br \/>{<br \/>    var heart = source <\/span><span style=\"color: #0000FF;\">as<\/span><span style=\"color: #000000;\"> Heart;<br \/>    <\/span><span style=\"color: #0000FF;\">if<\/span><span style=\"color: #000000;\"> (heart == <\/span><span style=\"color: #0000FF;\">null<\/span><span style=\"color: #000000;\">) <\/span><span style=\"color: #0000FF;\">throw<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">new<\/span><span style=\"color: #000000;\"> ArgumentException(<\/span><span style=\"color: #800000;\">\"The brain suppose to listen to heart only\"<\/span><span style=\"color: #000000;\">);<br \/><br \/>    heart.BeatEventHandler -= DeliverEvent;<br \/>}<br \/><\/span><\/pre>\n<p>The last part is the Heart where the event is getting fired. That is nothing special and can be implemented as before.<\/p>\n<pre><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;summary&gt;<\/span><span style=\"color: #008000;\"> Can beat and can e subscribed to the beat event handler <\/span><span style=\"color: #808080;\">&lt;\/summary&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #0000FF;\">public<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">partial<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">class<\/span><span style=\"color: #000000;\"> Heart<br \/>{<br \/>    <\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;summary&gt;<\/span><span style=\"color: #008000;\">Hanldler for notifying a beat  <\/span><span style=\"color: #808080;\">&lt;\/summary&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #000000;\">    <\/span><span style=\"color: #0000FF;\">public<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">event<\/span><span style=\"color: #000000;\"> EventHandler&lt;BeatArgs&gt; BeatEventHandler;<br \/><br \/>    <\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;summary&gt;<\/span><span style=\"color: #008000;\">Sends the beat through the events  <\/span><span style=\"color: #808080;\">&lt;\/summary&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #000000;\">    <\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;param name=\"beatArgs\"&gt;&lt;\/param&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #000000;\">    <\/span><span style=\"color: #0000FF;\">protected<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">void<\/span><span style=\"color: #000000;\"> OnSendBeat(BeatArgs beatArgs)<br \/>    {<br \/>        <\/span><span style=\"color: #0000FF;\">if<\/span><span style=\"color: #000000;\"> (BeatEventHandler != <\/span><span style=\"color: #0000FF;\">null<\/span><span style=\"color: #000000;\">)<br \/>            BeatEventHandler(<\/span><span style=\"color: #0000FF;\">this<\/span><span style=\"color: #000000;\">, beatArgs);<br \/>    }<br \/><br \/>    <\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;summary&gt;<\/span><span style=\"color: #008000;\">Create a beat and send it <\/span><span style=\"color: #808080;\">&lt;\/summary&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #000000;\">    <\/span><span style=\"color: #0000FF;\">private<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">void<\/span><span style=\"color: #000000;\"> BeatOnce()<br \/>    {<br \/>        OnSendBeat(<\/span><span style=\"color: #0000FF;\">new<\/span><span style=\"color: #000000;\"> BeatArgs());<br \/>    }<br \/>}<\/span><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Dealing with memory leak is not as far as some might think it is. But it doesn&#8217;t get serious until it is very late to change the fundamentals of our software design. In my recent project it happened to be caused by traditional event subscription pattern. Which is most likely not leaving any memory leak &hellip; <a href=\"http:\/\/panahy.nl\/index.php\/2011\/03\/11\/did-i-say-ive-got-memory-leak\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Did i say &#8220;I&#8217;ve got memory leak&#8221;?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[54,53],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":0,"uagb_excerpt":"Dealing with memory leak is not as far as some might think it is. But it doesn&#8217;t get serious until it is very late to change the fundamentals of our software design. In my recent project it happened to be caused by traditional event subscription pattern. Which is most likely not leaving any memory leak&hellip;","_links":{"self":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/58"}],"collection":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=58"}],"version-history":[{"count":0,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/58\/revisions"}],"wp:attachment":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}