3 ms·
In most managed-memory languages, try...finally (and using/try-with-resources which are based on it) handle exceptions just fine. The only problem that sometime
by tsimionescu 6y ago
In most managed-memory languages, try...finally (and using/try-with-resources which are based on it) handle exceptions just fine. The only problem that sometimes occurs is that exceptions thrown by a finally handler override the exception being bubbled, which can be bad if you wanted to handle that exception later on, but otherwise the resources get cleared properly. D has better behavior here, where throwing an exception on this path will automatically keep track of many in-flight exceptions.
Also, the problem of finalizers blocking is partly handled by the fact that finalizers are not run by the GC threads, but by a separate finalizer thread. Still, if a finalizer blocks, eventually you will run out memory, as all objects which require finalization will be pooling up.
Also, the re-animation behavior you describe is common to finalizers in all GC languages which expose them, it's not in any way specific to Managed C++ (C++/CLR). It is simply a consequence of how finalization works: when an object which requires finalization is eligible for GC, it is not collected, but moved to the finalization queue. Objects in that queue are still live objects. When the finalizer thread successfully runs the finalizer of such an object, it is simply removed from the finalization queue. If it's finalizer has added back a reference from some other GC root to the object, then it will still stay live. The only special property is that an object can't be finalized twice, so even if its finalizer attaches it to the live object graph once, when it becomes unreachable again it will be collected, it will not be put back in the finalization queue, it will be directly collected.
Basically, I don't think there is too much chance to find a valid use for this behavior, but it is very hard to prevent it without complicating the language significantly.
- twic 6y ago> Basically, I don't think there is too much chance to find a valid use for this behavior, but it is very hard to prevent it without complicating the language significantly. One approach is to provide a mechanism for cleanup that doesn't require the object to still be alive. Java has phantom references for this: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ref/PhantomReference.html https://docs.oracle.com/en/java/javase/11/docs/api/java.base... https://stackoverflow.com/a/43659951/116639 https://stackoverflow.com/a/43659951/116639 And i learn that Smalltalk introduced the "ephemeron", which is now present in some other languages: https://en.wikipedia.org/wiki/Ephemeron https://en.wikipedia.org/wiki/Ephemeron
- tsimionescu 6y agoSure, there are other possible cleanup mechanisms, and I believe Java is depreciating finalizers in favor of those other mechanisms. My point was only about finalizers themselves: a language with the finalizer mechanism I was describing above would need to add a lot of features to prevent finalized objects from becoming reachable again.
- masklinn 6y ago> One approach is to provide a mechanism for cleanup that doesn't require the object to still be alive. Java has phantom references for this: Java has pretty much deprecated finalizers due to how non-deterministic their run is. Modern java uses try-with-resources blocks which… are the exact same thing as Python's with.