Let’s say that you have the following scenario, you have an object in your hands that is similar to this one:
It holds some unmanaged resources, so you have to dispose it.
However, this is used in the following manner:
What is the problem? This object may be used concurrently. In the past, the frame was never updated, so it was safe to read from it from multiple threads. Now there is a need to update the frame, but that is a problem. Even though only a single thread can update the frame, there may be other threads that hold a reference to it. That is a huge risk, since they’ll access freed memory. At best, we’ll have a crash, more likely it will be a security issue. At this point in time, we cannot modify all the calling sites without incurring a huge cost. The Frame class is coming from a third party and cannot be changed, so what can we do? Not disposing the frame would lead to a memory leak, after all.
Here is a nice trick to add a finalizer to a third party class. Here is how the code works:
The ConditionalWeakTable associates the lifetime of the disposer with the frame, so only where there are no more outstanding references to the frame (guaranteed by the GC), the finalizer will be called and the memory will be freed.
It’s not the best option, but it is a great one if you want to make minimal modifications to the code and get the right behavior out of it.