Weak reference related exception
I started getting the exception below after starting to use a class from an open source MVVM framework that uses weak references to prevent memory leaks.
This class is called PropertyObserver
and is "A standardized way to handle the INotifyPropertyChanged.PropertyChanged
event of other objects. This class uses weak references and the weak-event pattern to prevent memory leaks."
The trigger for the exception was quitting a WPF app, in which a ShellVm
has a reference to a MasterVm
whic开发者_JS百科h has references to multiple DetailVm
s, each of which holds the aforementioned PropertyObserver
. The MasterVm
is ultimately the consumer of the static Inflector
class used to pluralize and capitalize this and that, which is where the WeakReference
related exception gets thrown.
I can't say I fully understand the exception, so I guess that is the starting point. How would I start to resolve this?
System.InvalidOperationException was unhandled by user code Message=Handle is not initialized. Source=mscorlib StackTrace: at System.WeakReference.set_Target(Object value) at System.Text.RegularExpressions.Regex.Replace(String input, String replacement, Int32 count, Int32 startat) at System.Text.RegularExpressions.Regex.Replace(String input, String replacement) at Inflector.Inflector.Rule.Apply(String word) at Inflector.Inflector.ApplyRules(List`1 rules, String word) at Inflector.Inflector.Pluralize(String word) at Smack.Core.Lib.TextUtil.StringEx.Pluralize(String s) at Smack.Core.Lib.TextUtil.StringEx.PluralizeWithCount[T](String s, IEnumerable`1 collection) at Smack.Core.Presentation.Wpf.ViewModels.MasterDetailVms.GenericMasterViewModel`2.get_Status() InnerException:
Will try to give a suggession, as something like this I met many years ago. To be honest I didn't find a real solution for it. First a rough one, after just change an architecture.
I'm afraid there is no other solution, then just try to call GC.Collect() on destroying object (rough solution), or just change MVVM framework.
After googling some:
Handle not initialized1
Handle not initialized2
In short this is something related to WeakReferences
bug present in different versions of .NET Framework
.
Good luck.
As noted in a comment:
The exception is actually triggered by Regex, it also uses weak refs. The stack trace is very incomplete. Quacks like a bug in the library code. Look for destructors being use inappropriately.
Hans Passant, Sep 29, 2011 at 19:24
And this was confirmed by the OP:
That was it, some logging line in a destructor where a value was null
Berryl, Sep 29, 2011 at 20:54
精彩评论