Tag Info

New answers tagged

0

Runtime errors are not a tool to discipline programmers. they are an ultima ratio to deal with unexpected errors which, if not handled, would leave an unstable state. Which is not the case with open file handles. In the grand scheme of things, it doesn't matter when they get closed. Should the compiler issue a warning, if it finds unmanaged resources going ...


3

In Java 7 and later, resources that implement AutoCloseable will cause the compiler to emit a warning when a resource is not managed with try-with-resources. Combined with garbage collection, this goes a long way toward ensuring that resources are freed at some point. As Robert Harvey mentioned, the operating system also disposes of a process's entire ...


2

What would happen if a language threw run-time errors e.g. on leaked file handles? Coders would hate it with a flaming passion, and it would go nowhere. The point of resource management is to aid you in producing a valuable program with less effort. "Valuable" doesn't mean "perfect": a program that leaks a file handle is bad, but often it is nowhere near as ...


2

The runtime can detect that an open file has not been closed. The only time the runtime can unambiguously determine that an open file should have been closed is upon process shutdown, particularly with event driven frameworks that may reuse open file handles in callbacks, etc. Unless the handle has been scoped (and you said ignore C# using {} and Java ...


2

While it has almost certainly been considered, it has been discarded because, in the general case, it is impossible to do. What happens if f is not a local variable, or is returned from the function? At that point, you have an equivalent to the Halting Problem. There is no way (in the general case) for static analysis to prove one way or another if the ...


0

The destructor is called after the object goes out of scope. If f is global, then that means at program exit. Someone could use that resource f again, and the compiler / runtime is not generally in a position to know for sure that that cannot happen in any particular case. Its good practice to scope variables as tightly as possible, and avoid globals. ...


1

When a process is terminated, all of the allocated memory gets automatically released back to the operating system, and any open file handles are closed. That's your safety valve. The problem, of course, is that the operating system (and your compiler, for that matter) doesn't actually know that your example code is an error. You have to state, by your ...



Top 50 recent answers are included