Re: is such exception handling approach good?
George schrieb:
I am confused. If all the occupied resources (including memory) will be
automatically released when the process exits, why there are any cases which
will cause memory leak? (I think there is no memory leaked when the process
exits.)
Memory is freed when when the application exits. But as long as the application
does not exit, you are responsible for your memory. Some programs run a long
time. Imagine MS Word would leak a few bytes eevy time you move the mouse or hit
a key, you would have to restart it every now and then). Or think of system
services which never end except you rstart them manually or shut down the OS.
And there can be other types of resource leaks. You could have locked a global
semaphore that was created by some other process. When your application shuts
down, the lock would not be released automagically.
So having resource leaks is bad, even if it does not cause harm in some cases.
Even minor resource leaks can add up over time, creating a huge impact on the
user or the OS. Sometimes it is ok to not free resources at program shutdown,
but usually only for resources that only exist once (like configuration data
that is read once at appliction start). But you never know what will happen to
your code in the future, so you'd better always clean up after yourself.
Norbert