ResExtension will introduce memory leak by continuing Open/Close the window

Jul 20, 2009 at 3:59 AM

Hi all:

    The ResExtension will register LocalizationSettings.CultureChanged event which will hold a strong reference to the ResExtension.

    I tried to use WeakEvent reference to solve it, but it'll be GCed after the Window is loaded, do you have a good solution for this?



Bai Shi

Jul 20, 2009 at 7:27 AM
Edited Jul 20, 2009 at 7:28 AM

Yes I know about this issue.

You can set the static ResExtension.CheckForCultureChange property to false in initialization code which will prevent events from being hooked up and avoid the issue altogether. However, this also prevents you from auto-detecting culture changes, but as outlined in the article in general this is bad practice anyway and difficult to manage for EVERYTHING in the application. If there's anything that happens not to be bound by the ResExtension it won't update and you'll have a mismatched UI.

The other thing you can try is forcing the GC to collect on occasion. The ResExtension instance has a Destructor, which should eventually fire but because of non-deterministic cleanup doesn't fire immediately when the instance goes out of scope. Firing the GC to collect should at some point collect the released instances and force the destructor to fire.

Unfortunately I don't have a better answer than this - because of the event driven nature of this process and the fact that each bound control gets its own ResExtension instance that is instantiated by the control, there's no effective way that I know of to clean up these events references.

IMHO the first approach - not allowing on the fly culture changes - is the easiest and most reliable way to dealing with this.

Hope this helps,

+++ Rick ---

Jul 20, 2009 at 11:50 AM

The ObjectDependencyManager can mitigate the impact of ResExtension memory while still have the Dynamic Culture change feature