Is there a way to return all unrefrenced objects?
I have this "Linked Web"(?) data structure ie. each object has a bunch of references to other objects.
So i wrote a method that is supposed to 'remove' a passed object by removing all references to it.
I need to test it and make sure that after i run the method the particular object i want to remove is not referenced by anything else
How could I do this?
An idea would be to force a Garbage Collection then run my delete object method and then force another GC to see if it found an object.
if it found an ob开发者_StackOverflow社区ject for deletion then I would assume that my method works but if it found nothing to collect then I would assume that something is referencing it and would have to plug that leak
Is this possible? How?
Thanks, Ryan
You can keep track of each object via a WeakReference
and check the IsAlive
property after a garbage collection.
This strikes me as an overly convoluted way to test whether your logic is correct. I would construct a couple of unit tests in the following manner.
- Add an object to the web. In the test keep a reference to the object
- Call the logic that unhooks the reference from the web.
- Iterate over (or traverse) your web and use the ObjectReferenceEquals to see if the reference you're looking for is still hanging around.Since you have numerous possible connections, this may not be the swiftest of operations. However in a relatively small test it should be fine.
There is nothing in your scenario that leads me to believe you need to leverage garbage collection to test your data structure. Also, I believe your data structure is correctly termed a graph. Good luck
精彩评论