2

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 which has references to multiple DetailVms, 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: 
Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
Berryl
  • 12,471
  • 22
  • 98
  • 182
  • what version of .NET do you use and what version uses that framework? – Tigran Sep 29 '11 at 18:37
  • @Tigran. All of my assemblies are 4.0. I copied the code into an existing assembly, so no references to a framework dll for the PropertyObserver. The Inflector code that throws is in a dll, and likely an older version (not sure how to tell for sure though) – Berryl Sep 29 '11 at 18:50
  • 5
    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 '11 at 19:24
  • @HansPassant. That was it, some logging line in a destructor where a value was null - thank you!. Why not put this in an answer format so I can mark it as such? Cheers – Berryl Sep 29 '11 at 20:54

2 Answers2

2

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.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • That doesn't sound encouraging... sigh, another day – Berryl Sep 29 '11 at 19:18
  • @Berryl: Sad, but true. May be you can download it, recompile under .NET 4.0, and hopefully "resolve" your problem... – Tigran Sep 29 '11 at 19:34
  • 1
    I actually did that before I saw the comment from Hans. The problem was a rogue destructor that the WeakRef somehow exacerbated – Berryl Sep 29 '11 at 22:13
0

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

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81