简体   繁体   中英

WPF Dispatcher Operations Leak

I'm seeing a memory leak in our WPF application. In using .NET Memory Profiler, I can see that even after closing a window, the DataContext and UI elements are all sticking around because of Action delegates that are in DispatcherOperations in the Dispatcher queue...but have a ContextIdle priority and never seem to get processed.

Can I force the Dispatcher queue to flush when my Window gets closed?

You could try to include a synchronous call to a dummy method using dispatcher.Invoke with priority ContextIdle in the Window.Closing event. This should stop the UI thread from generating more dispatcher items, and should return only after the last ContextIdle items are processed. However, I couldn't verify that since without the Telerik component ContextIdle items get processed before the window closes, anyway.

I could also imagine that those items run in a loop, ie create themselves anew during their execution. In that case, it might help to set the DataContext explicitly to null before you close the window. This might signal to the components that they can stop their loop, and at least you won't leak instances of your datacontext objects this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM