How to remove DependencyProperty Callback event handler to avoid memory leak RRS feed

  • Question

  • After reading this MSDN article on how to hook up a DependencyProperty's Callback method to know when its value changed, I am wondering how I unhook the Callback event handler to avoid a memory leak.  Or will there not be a memory leak since both the Dependency Property and the Event Handler are defined to be static?

    Here is an example of the code I am talking about:

    public virtual int SelectedID
    	get { return (int)GetValue(SelectedIDProperty); }
    	set { SetValue(SelectedIDProperty, value); }
    public static readonly DependencyProperty SelectedIDProperty =
    	DependencyProperty.Register("SelectedID", typeof(int), typeof(SelectorBase), new PropertyMetadata(0, new PropertyChangedCallback(OnSelectedIDChanged)));
    private static void OnSelectedIDChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    	// Record the old value of the Selected ID
    	(d as SelectorBase)._lastSelectedID = (int)e.OldValue;


    They don't mention anything about unhooking the events, which is typically done in a Dispose() function when following the Dispose() pattern.  So do I need to worry about unhooking these event handlers?  and if so, how do I go about doing it when hooking the events up this way?  Thanks in advance.



    Tuesday, November 16, 2010 5:35 PM


  • Hi Deadlydog,

    Based on my understanding, you don't need to unhook the PropertyChangedCallBack delegate since it is static.

    Best regards,


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by deadlydog Thursday, November 18, 2010 3:27 PM
    Thursday, November 18, 2010 7:15 AM

All replies