I have got two classes, one is Device and the other is Buffer. Every Buffer
instance has an associated Device instance, since all Buffers have to be
created with Device.CreateBuffer(). Now i want to be able to notify an event
to all Buffer instances associated with a certain Device. At the same time i
would like the Buffer instances to be garbage collected, if there is no
reference from the client code (the one that called Device.CreateBuffer)
anymore. The problem is that the Device instance still holds a reference to
the otherwise unreferenced Buffer, preventing it from being garbage
collected.
I could solve the problem with a Destroy() method on the Buffer class, that
deletes itself from the notify-list of the Device instance. But having this
explicit destroy instruction is not really nice in a managed environment.
Someone's told me about WeakReference objects, but they look more like a
workaround than a solution. Any hints?