Receive notification of deletion / destruction of objects

I need a way to track instances of various classes, without these classes not knowing that they are being tracked. Essentially, I have a factory class that creates instances and passes them to another thread. After this thread completes and unloads the instance, I need to get a notification about this so that I can perform reference counting and exit my factory class when all instances have disappeared.

The problem is that I cannot change any of the classes that I will load, because I do not control their source code.

Keeping track of the instances I create is simple, I can just put them in some kind of collection when I create them. Tracking their destruction is causing me problems. If I could change the source code, I would add an event to each class, and when I create an instance, I hooked on this event and used it as my notification. But I can’t do it.

So, the question is: is there a hidden way to control an instance of an object and detect when it is destroyed?

+7
c # clr object-lifetime
source share
4 answers

There is no way to get an active notification, but you can save the WeakReference object for objects and periodically check if they have died.

Edit: I like that Reed is better than mine!

+3
source share

Since you are creating objects, it seems that you can return the Decorator instead of the actual instance.

Using the Decorator Pattern , you can wrap the returned object in your own decorated API. Decoration can provide an implementation of IDisposable, which provided your notice of destruction.

+10
source share

If you keep a list of links to instantiated instances, they will not receive garbage collection and therefore will never be destroyed ...

Instead, you can create a repository that contains a list of Guides and create a new Guid for each instance, and then add it to the list instead β€” something like FactoryRepository. Thus, you do not have a reference problem for garbage collection, since Guides are structures, not reference types. You can then inherit from each class to create a type that can notify about destruction. I assume that since you cannot change the code of the source classes, you also cannot change the consumers of these classes, so something like the decorator pattern (via the interface) is missing because the types will not be compatible.

A very simplified example:

 public class OriginalClassDestroyNotifier : OriginalClass { private readonly Guid _instanceId; public OriginalClassDestroyNotifier(Guid instanceId) { _instanceId = instanceId; } ~OriginalClassDestroyNotifier() { FactoryRepository.NotifyDestroyed(_instanceId); } } 
+2
source share

How about controlling the destruction of an object:

 public void Destroy(T obj) { if (obj == null) throw new ArgumentNullException("obj"); if (!_living.Contains(obj)) throw new ArgumentException("Where did this obj come from?"); using (obj as IDisposable) { } _living.Remove(obj); // List? } 
0
source share

All Articles