0

I am surprised not being able to find anything on the subject.

Garbage collection is not deterministic (it will occur some times later). Does that mean what weak event handlers may (will?) continue being called for object which is not referenced? This could be a reason of problems (performance, disposed object state, etc.), right?

If I must use weak events, is it a good idea (is it enough?) to call GC.Collect() explicitly to avoid such problems?

P.S.: I have performance problem with some software and trying to fill gaps in my knowledge before attempting something more time-consuming.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • It is a WPF-specific pattern, trading a big memory management problem caused by data binding for a smaller one. It still requires management, those WeakReferences that are !Alive need to be destroyed too. But it is okay for that to build up for a while since they are small. The dispatcher takes care of it when nothing else needs to be done, at DispatcherPriority.ContextIdle. GC.Collect() has no obvious relevance, this doesn't look like a WPF question. – Hans Passant May 10 '17 at 12:32
  • @HansPassant, in my case dispatcher doesn't take care of such objects, they stay, event handlers are still called. See linked question, I've managed to reproduce issue (and even face other problem with lambda as weak event handler). Without calling `GC.Collect()` everything is very bad with weak events. – Sinatr May 10 '17 at 12:56

0 Answers0