5 ms·
The preferable options in these sorts of situations generally boil down to: 1. Use a SafeHandle or CriticalHandle 2. Let the finalizers for the resources y
by bunderbunder 11y ago
The preferable options in these sorts of situations generally boil down to:
1. Use a SafeHandle or CriticalHandle
2. Let the finalizers for the resources you're holding take care of themselves.
#2 because if your resources already have their own finalizers (e.g., streams, db connections), then implementing your own is like a belt and suspenders. Except that whichever one you added 2nd is probably broken and makes everything strictly worse.
- StillBored 11y agoSure, there are other strategies for handling the problem. But many have latent bugs, which is mostly what this article is pointing out. In your case 2, unless you try to force the finalizer to run (he has a collect/waitforpendingfinalizers sequence in one of his examples) this potentially leaves the resource consumed for an indeterminate amount of time. Its also not clear to me that the *Handle routines guarantee correct behavior with processes running outside of the CLR. I just looked at the docs and it reenforced my understanding that they seem to be strictly designed to protect .net applications from shooting themselves in the foot. I'm not a .net/CLR expert so take that for what its worth.
- deleted 11y ago[deleted]