What, again?
Posted May 2, 2007 0:58 UTC (Wed) by
bronson (subscriber, #4806)
In reply to:
What, again? by peterh
Parent article:
The Rise of Functional Languages (Linux Journal)
What does a destructor provide that finalize() doesn't? That's easy: a definite time that the cleanup code is called. This argument is ages old. Briefly:
for(log files) {
File f;
f.open(file name);
f.quick_scan();
}
In C++, you will only have a single fd in flight because the destructor is called and the file is closed the moment the block is exited. In Java (older versions; I haven't used Java recently), you'd typically bomb out after a few thousand iterations because you exhaust your file descriptors before a garbage collection cycle can be run.
Most resources are of such a limited quantity that you need to know with confidence exactly when they are released. Destructors provide this, finalizers don't.
For instance, do you see the problem with the following code? It's only a problem if your language uses finalizers.
try {
FileReader f = new FileReader(filename);
process(f);
r.close();
} catch(IOException e) {
...
}
(
Log in to post comments)