Problems upon hosting WPF controls on WinForms RRS feed

  • Question

  • Hi all,

    I have following problem, my product has two different clients. One is WinForms application and another one is WPF application. Some screens (WPF user controls) used on both of these clients. I found the memory leak in WinForms application, every WPF user control ever shown remains in memory and it is problem, because we use stateless model and each screen (control) is created from scratch every time. The problem exists only on WinForms client, the same controls works fine in WPF application. I found with .Net memory profiler that somehow WPF resources holds references to WPF controls. All our styles, templates and etc are concentrated in one assembly.

    The solution I use currently to solve the problem, when WPF control is closed just clear all resources and recreate them

    wpfApplication.Resources.MergedDictionaries.Clear( );
    foreach( string resourceLocation in resources )
     ResourceDictionary resourceDictionary = new ResourceDictionary( );
     resourceDictionary.Source = new Uri( resourceLocation, UriKind.Absolute );
     wpfApplication.Resources.MergedDictionaries.Add( resourceDictionary );


    It solves the memory leak, but sometimes there are exceptions in this code, for example: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.

    at System.Collections.Hashtable.HashtableEnumerator.MoveNext()  
    at System.Windows.ResourceDictionary.SealValues()  
    at System.Windows.ResourceDictionary.AddOwner(DispatcherObject owner)  
    at System.Windows.ResourceDictionary.PropagateParentOwners(ResourceDictionary mergedDictionary)  
    at System.Windows.ResourceDictionary.OnMergedDictionariesChanged(Object sender, NotifyCollectionChangedEventArgs e)  
    at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)  
    at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item)  
    at System.Windows.ResourceDictionaryCollection.InsertItem(Int32 index, ResourceDictionary item)  

    The questions are:

    1 Why the same code works fine in WPF native application and there are memory leaks in WinForms? (We share dll between these clients, and there is no difference in resources)

    2 Is there safe way to clear and refill resources?

    Best Regards, Sergey
    Thursday, December 23, 2010 8:08 PM

All replies

  • Hi Sergey Karpov,

    Welcome to MSDN forum!

    Based on your description, there isn't enough information for us to make a guess at the root cause.

    Could you kindly offer more details about your issue? It would be appreciated if you could offer a simple ready-to-run sample for us.


    Thank you! Willing to help you!


    Best regards,


    Yves Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 27, 2010 4:08 PM