RadWindow not disposing Content?

3 posts, 0 answers
  1. Happy
    Happy avatar
    63 posts
    Member since:
    Aug 2008

    Posted 21 Aug 2009 Link to this post

    I'm creating a RadWindow for a modal dlg. I'm putting a custom control into the RadWindow Content. property Everything works great.

    That custom control is hooked into some global events. After I close the RadWindow modal dlg, the events in that custom control are still firing. It's like my custom control still exists in memory and wasn't disposed.
  2. Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 24 Aug 2009 Link to this post

    Hi Happy,

    Usually you should detach the event handlers when you don't need them, because the class that raises the events will keep the reference to the handler until it is disposed, hence the classes/controls that declare/attach the event handlers will live longer. If you do not keep a reference to RadWindow it should properly be disposed, as well as its content. You can test this with the following code, where SilverlightControl2 is just a UserControl with any content:
    <Button Content="Show window" Click="Show_Click" />
    <Button Content="Check disposed" Click="Check_Click" />

    private void Show_Click(object sender, RoutedEventArgs e)
    {
        
    RadWindow window = new RadWindow();
        window.Closed += window_Closed;
        window.Content =
    new SilverlightControl2();
        this.control = new WeakReference(window.Content);
        window.Show();
    }

    void window_Closed(object sender, WindowClosedEventArgs e)
    {
        RadWindow window = sender as RadWindow;
        window.Closed -= window_Closed;
    }

    private void Check_Click(object sender, RoutedEventArgs e)
    {
        
    GC.Collect();
        
    Debug.WriteLine(this.control.IsAlive);
    }

     


    Sincerely yours,
    Valeri Hristov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. DevCraft banner
  4. Happy
    Happy avatar
    63 posts
    Member since:
    Aug 2008

    Posted 24 Aug 2009 Link to this post

    Thanks. Yesterday this was driving me nuts. Now today, I can't reproduce it.

    But at least now I've learned about "WeakReference"
Back to Top