I am working on a big project that has lots of classes and those classes has incoming references. Before we were using the .NET Framework, we were using the WeakReference class to see if the object got disposed. Recently we migrated our project to .net core 5, and all of a sudden that test with object disposing test started to fail. After a little bit(quite a bit actually) research, find out that WeakReference doesn't function the same way as it did in .NET Framework.
Student student = new Student();
WeakReference weakRef = new WeakReference(student);
student.Dispose();
student = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
var isAlive = weakRef.IsAlive;
Any ideas how I can test whether the object got disposed properly? I could put some kind of a log in the disposing method, but that still doesn't guarantee that the object is disposed. Purpose of the question is not to test whether .net core disposing my object, I would like to know if there are other objects referencing to it that is preventing the object from being disposed.
PS: I also put Thread.Sleep with while loop to see if the object will get disposed later on, but that didn't happen even after 30 mins.
I tried using the JetBrains dotMemoryUnit, and that also acted the same as WaekReference. It is a great tool that shows all the issues and eliminating the problem is so quick.