This is a migrated thread and some comments may be shown as answers.

RadWindow not disposing Content?

2 Answers 199 Views
Window
This is a migrated thread and some comments may be shown as answers.
Happy
Top achievements
Rank 1
Happy asked on 21 Aug 2009, 10:58 PM
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 Answers, 1 is accepted

Sort by
0
Valeri Hristov
Telerik team
answered on 24 Aug 2009, 10:22 AM
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.
0
Happy
Top achievements
Rank 1
answered on 24 Aug 2009, 05:23 PM
Thanks. Yesterday this was driving me nuts. Now today, I can't reproduce it.

But at least now I've learned about "WeakReference"
Tags
Window
Asked by
Happy
Top achievements
Rank 1
Answers by
Valeri Hristov
Telerik team
Happy
Top achievements
Rank 1
Share this question
or