tags:

views:

47

answers:

2

Hello,

I want to know when I am programming an application, say both unix and windows. If program stops (got an exception), what resource I should release ?

objects on the head ? DB connections ? file descriptions ? shared memory ?

If I am not mistaken unix releases all except from shared memory and temp files, right ?

Is there something else that should taken into consideration ?

Thanks,

+2  A: 

It's an operating system's job to reclaim system resources if a program ends abruptly (or properly for that matter). Most OSs these days do that job fairly well.

What you should be more concerned about is corruption and data loss. Might your program terminate halfway through saving a file? Could some data be lost? Is your database going to be in an inconsistent state? Is other software you're communicating with going to hang/crash?

Also consider what sort of reasons your program will fail, how likely they are, and how much effort it will be to deal with them properly.

Artelius
+2  A: 

On any modern OS the heap, open files, and open connections are process resources and will be released on process termination. If you explicitly created a temporary file or shared memory then they will continue to exist until you explicitly remove them, since these are obviously not process-specific.

Something that you can do on Unix filesystems is create/open your temporary file and then immediately unlink() it. No one else will be able to open it after that but you can still read and write to the file as long as you have it open. If you are sharing the temporary file among multiple processes then you can unlink it after the final process has opened the file. This has the advantage that there will be no need to remove the file on program termination; the disk space will automatically be reclaimed once there are no further references to it and it is no longer open in any process, even if the process was forcibly killed. Windows does not allow you to unlink the file until all processes have closed it, which could make cleanup difficult especially if multiple processes are sharing the same file.

mark4o
+1 for the unlink() trick. Good ol' Unix.
Artelius