WPF Events and memory leaks

26 ביולי 2007

אין תגובות

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/wpf-events-and-memory-leaks/]


Today, we'll speak about RoutedEvent and possible memory leaks associated with them. If you ever use EventManager, that's really cool mechanism of external attached event handling, you'll notice about lack of ability to unsubscribe from event handlers. What to do? Is it bad design? Actually, yes. This is possible memory leak. So, what to do with it?


First of all, you can try to null handler.


    
if(em_handler == null)

em_handler =
new RoutedEventHandler(EM_HandlePreviewMouse);

EventManager.RegisterClassHandler(typeof(Button), TextBlock.PreviewMouseDownEvent, em_handler);



em_handler = null


 

 



This will not work. Actually, the reference remains, event if handler is nulled. So, what can we do? We can use attached events. As well as we are able to attach to external event, we can unattach from it


    
if(re_handler == null)

re_handler =
new RoutedEventHandler(RE_HandlePreviewMouse);

this.AddHandler(Button.PreviewMouseDownEvent, re_handler);


this.RemoveHandler(Button.PreviewMouseDownEvent, re_handler)


          

 





So, this can give us possible solution, but what to do with really large objects? Unattaching from events will net destroy references to them. Actually, even in .NET 2.0 and 1.1, when we're using -= operator, we are not disposing handlers, we only disconnect from it.


In WPF where is new cool class, named WeakEventManager and it's implementation IWeakEventListener. But how to use them in our case? For real it's rather simple. Create new object, derrived from WeakEventManager for class you want to handle events. Just like this


    
public class WeakButtonEventManager:WeakEventManager

{

protected override void StartListening(object source)

{


Button b = source as Button;

if (b != null)

{


b.PreviewMouseDown += new MouseButtonEventHandler(OnPreviewMouseDown);

}


}


protected override void StopListening(object source)

{


Button b = source as Button;

if (b != null)

{


b.PreviewMouseDown -= new MouseButtonEventHandler(OnPreviewMouseDown);

}


}



void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)

{


DeliverEvent(sender, e);


}



public static void AddListener(Button source, IWeakEventListener listener)

{


Manager.ProtectedAddListener(source, listener);


}



public static void RemoveListener(Button source, IWeakEventListener listener)

{


Manager.ProtectedRemoveListener(source, listener);


}



static WeakButtonEventManager Manager

{


get

{

Type t = typeof(WeakButtonEventManager);

WeakButtonEventManager m = WeakEventManager.GetCurrentManager(t) as WeakButtonEventManager;

if (m == null)

{


m = new WeakButtonEventManager();

WeakEventManager.SetCurrentManager(t, m);

}


return m;

}


}


}</PRE< P>

 




Then, create it's weak event implementation


    
public class ExpensiveButton : DispatcherObject, IWeakEventListener

{

Button b;

public event MouseButtonEventHandler PreviewMouseDown;


public ExpensiveButton(Button source, bool isReallyExpensive)

{


b = source;


if(isReallyExpensive)

WeakButtonEventManager.AddListener(b, this);

else

b.PreviewMouseDown += new MouseButtonEventHandler(OnPreviewMouseDown);

}



~ExpensiveButton()


{


this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (SendOrPostCallback)delegate

{

//Clean up all resources


WeakButtonEventManager.RemoveListener(b, this);

b.PreviewMouseDown -=
new MouseButtonEventHandler(OnPreviewMouseDown);

},
null);

}



void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)

{


if (PreviewMouseDown != null)

PreviewMouseDown(sender, e);


}


#region IWeakEventListener Members


public bool ReceiveWeakEvent(Type managerType, object sender, EventArgs e)

{


if(managerType == typeof(WeakButtonEventManager))

{


OnPreviewMouseDown(sender, e as MouseButtonEventArgs);

return true;

}


return false;

}



#endregion

}</PRE< P>

 




Now, you can easily handle and unhandle this class event, by disposing the event itself, rather then leave it in stack.


    
ex_button = new ExpensiveButton(butt, true);

ex_button.PreviewMouseDown +=
new MouseButtonEventHandler(EX_HandleMouseDown);

….

ex_button.PreviewMouseDown -= new MouseButtonEventHandler(EX_HandleMouseDown)


 


 


Happy programming


Source code for this article

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *