views:

55

answers:

2

I am creating window in another thread. After closing the thread some of the resources window is not released from the memory. Because of this growing counter GDI Objects and User Objects in windows task manager. Graphics that not released are font and region. I haven't idea what is going on...

public class WaitingWindowManager
{
    private Thread thread;
    private bool canAbortThread = false;
    private Window waitingWindow;

    public void BeginWaiting()
    {
        this.thread = new Thread(this.RunThread);
        this.thread.IsBackground = true;
        this.thread.SetApartmentState(ApartmentState.STA);
        this.thread.Start();
    }

    public void EndWaiting()
    {
        if (this.waitingWindow != null)
        {
            this.waitingWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => { this.waitingWindow.Close(); }));
            while (!this.canAbortThread) { };
        }

        this.thread.Abort(); 
    }

    public void RunThread()
    {
        this.waitingWindow = new Window();
        this.waitingWindow.Closed += new EventHandler(waitingWindow_Closed);
        this.waitingWindow.ShowDialog();
    }

    void waitingWindow_Closed(object sender, EventArgs e)
    {
        this.canAbortThread = true;
    }
}

And call :

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();  
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
            WaitingWindowManager waitingWindowManager = new WaitingWindowManager();
            waitingWindowManager.BeginWaiting();
            Application.Current.Dispatcher.Thread.Join(5000);
            waitingWindowManager.EndWaiting();
    }
}
A: 

Add Dispatcher.CurrentDispatcher.InvokeShutdown(); in your closing code. That should take care of any leaking memory.

Femaref
It's WPF not windows forms at first and at second I did not creategdi ojects
LuckyDan
It's not work. The window (waitingWindow) is destroyed, this can be seenin memory profiler, but the object of the WaitingWindowManager class can't be destroyed. And such behavior appears only after creating WPF window in the thread
LuckyDan
+1  A: 

Remove your Closed eventhandler in your waitingWindow_Closed Event. It is causing your window to not be disposed. If you manually add events you need to make sure you remove them when finished.

I also noticed another Stackoverflow question that was pertaining to memory leaks in wpf. It referenced this article maybe this will help you.

Mark Hall
Not work. I have a leak memory in this case too. public void RunThread() { Window waitingWindow = new Window(); // waitingWindow.Closed += new EventHandler(waitingWindow_Closed); waitingWindow.ShowDialog(); }
LuckyDan
What I did was this. I used the suggestion by Femaref and removed the handler. I does allow the new window to be disposed. void waitingWindow_Closed(object sender, EventArgs e) { this.waitingWindow.Closed -= new EventHandler (waitingWindow_Closed); Dispatcher.CurrentDispatcher.InvokeShutdown(); this.canAbortThread = true; } If this is still leaking what objects are being leaked
Mark Hall
I can't understand. Assume that eventhandler prevents us from disposing object. And if I add void waitingWindow_Closed(object sender, EventArgs e) { this.waitingWindow.Closed -= new EventHandler(waitingWindow_Closed); this.canAbortThread = true; } it's must to resolve the problem. But it's not work.Moreover, if you haven't the handler in general, the result is the same one : leak of the memory
LuckyDan
RE: If this is still leaking what objects are being leakedThe window (waitingWindow) is destroyed, this can be seen in memory profiler, but the object of the WaitingWindowManager class can't be destroyed. And such behavior appears only after creating WPF window in the thread
LuckyDan
In the memory profiler that I am using. It is showing the WaitingWindowManager class being destroyed. SciTech .Net Memory Profiler(http://memprofiler.com).
Mark Hall