Herman: Shipping Rust in Firefox
Herman: Shipping Rust in Firefox
Posted Jul 20, 2016 6:55 UTC (Wed) by oever (guest, #987)In reply to: Herman: Shipping Rust in Firefox by mathstuf
Parent article: Herman: Shipping Rust in Firefox
If I have a temporary directory, I might like it to be cleaned up no matter how the function exits (except things like poweroff, abort() or SIGKILL; not much anyone can do there).In C++, you can clean up in the destructor of an object. This is simple RAII. In Java, you could clean up in the finalize() function. The C++ destructor is called immediately when the scope ends. finalize() is called by the garbage collector which runs a few times per second. In Java and C# most variables are pointers to objects and Java does not have a way to automatically call a function directly when the last pointer to an object leaves scope and neither does C#.
A refreshing aspect of Rust is that the syntax for dealing with objects is nice and safe. It has the best of both worlds: no need for manual memory deallocation but the ability to run any type of cleanup when the last reference to an object leaves scope. Defining a destructor is optional and done via the Drop trait.
Posted Jul 20, 2016 7:01 UTC (Wed)
by Cyberax (✭ supporter ✭, #52523)
[Link] (1 responses)
The way to manage resources in Java now is try-with-resources statement.
Posted Jul 20, 2016 9:12 UTC (Wed)
by farnz (subscriber, #17727)
[Link]
It's not even guaranteed to be called in Java; it's called if the GC determines that this object has no references and is thus eligible for collection.
In practice, this means that if your process exits before the GC decides that your object is ready for collection, the finalizer is not called at all - e.g. because there happens to have been no memory pressure between the object being allocated, and the process exiting cleanly for a restart.
Herman: Shipping Rust in Firefox
No, you can not. Finalize method is only guaranteed to run some time in future - like next hour or next day.
Herman: Shipping Rust in Firefox