views:

170

answers:

4

From this answer: http://stackoverflow.com/questions/1058797/when-is-a-c-terminate-handler-the-right-thingtm/1058894#1058894

It would be nice to have a list of resources that 'are' and 'are not' automatically cleaned up by the OS when an application quits. In your answer it would be nice if you can specify the OS/resource and preferably a link to some documentaiton (if appropriate).

The obvious one:

Memory: Yes automatically cleaned up. Question. Are there any exceptions?

+3  A: 

Any exception is a bug - applications can and do crash and do contain leaks. An OS needs to be reliable and not exhaust resources even in the face of poorly written applications. This also applies to non-OS resources. Services that hand out resources to processes need to free those resources when the process exits. If they don't it is a bug that needs to be fixed.

If you're looking for program artifacts which can persist beyond process exit, on Windows you have at least:

  • Registry keys that are created without REG_OPTION_VOLATILE
  • Files created without FILE_FLAG_DELETE_ON_CLOSE
  • Event log entries
  • Paper that was used for print jobs
Michael
Yes that is the ideal. But not all resources are owned by the OS.
Martin York
@Martin - your title says OS resources :) Even then, it is a bug in the component or service that allocated the resource.
Michael
Which resources are not owned ultimately by the OS?
anon
@Neil - it depends on what you define as OS. Is a COM server running in a separate process that was created by you via CoCreateInstance an OS managed resource? (in this case, the COM subsystem will shut down the server if you exit)
Michael
Well, I certainly don't define a COM server as the OS. COM is just a server architecture, in the same way that Apache is. But this is off the point, I feel.
anon
File handles are owned by the OS, but files are resources owned by the File System. A processes may be owned by the OS but the service running inside the processes (aka a DB) is not owned by the OS.
Martin York
@Michael: Editied the title. Hope the ';' clears up my menaing.
Martin York
@Martin I hate to say it, but I think you need to read up on OS architectures. A file system is a way of structuring an OS resource - the resource is still owned by the OS. F
anon
I hate to say but I disagree :-) A FS is somthing the OS may or may not need and as such a potential resource. But it inherentely has its own state and resources that it manages indepedntely from the OS. This becomes very aparent when you phisically seporate a FS from the OS (think of an OS that only has a FS via network storage).
Martin York
@Martin In the case of a networked resource like NFS the file system is still owned by the operating system on the NFS server.
anon
@Neil Butterworth: No disagreement there. My point still holds up.
Martin York
+3  A: 

In Windows, just about anything you can get handle to should be in fact be managed by the OS - that's why you only get a handle. This includes, but is not limited tom the following (list copied from MSDN docs for CloseHandle() API):

Communications device 
Console input 
Console screen buffer 
Event 
File 
File mapping 
Job 
Mailslot 
Mutex 
Named pipe 
Process 
Semaphore 
Socket 
Thread 
Token

All of these should be recovered by the OS when an application closes, though possibly not immediately, depending on their use by other processes.

Other operating systems work in the same way. It's hard to an imagine an OS worth its name (I exclude embedded systems etc.) where this is not the case - resource management is the #1 raison d'etre for an operating system.

anon
We can agree tha anything controlled via a handle will be eventually be released by the OS.
Martin York
It depends what you mean by released. A crashed process cannot hold a Mutex (another process waiting on the Mutex will get ownership with a WAIT_ABANDONED). But it's different for a Semaphore. If another process is waiting on that, it's hung. If you kill the hung app, then the system reclaims the Semaphore object.
Adrian McCarthy
I have an app that, when it crashes, routinely leaves behind a zombie console window (it's a Windows app the also creates a console window). I have to reboot periodically to clean them all out.
Adrian McCarthy
+3  A: 

Temporary files is a good example of something that will not be cleaned up - the handle is released but the file isn't deleted

Paul Betts
A good point. But temporary files are like outstanding print jobs - the OS simply doesn't have the information to decide whther they should be deleted immediately a process terminatyes. But they will all get reaped/cleared/flushed/deleted at some point.
anon
One could argue that everything will eventually be cleaned up. The problem is that untidy resource deallocation can lead to other problems. In this case we may run out of free space in the file system until the files are repeated by a cleaner processes.
Martin York
But it's obviously not the case that a process should delete its temporary files (or that the OS should). Consider your word-processor backup files - you don't want to lose them when Word (or whatever) panics.
anon
@Neil: You point? It is a resource that is not cleaned automatically up by the OS. Given the opertunity it should have been cleaned up by the app, but was leaked because of a problem. That I believe is the whole point of this discussion.
Martin York
What's the use case for temporary files that shouldn't outlive the process? The only (mis)use I see is as a sort of secondary memory -- but you should be relying on your OS' virtual memory management to do that for you.
Joseph Garvin
To view pdf's generated from program data they were saved to temporary files in one program I worked on.
CiscoIPPhone
@Joseph: Temporary files can be used to cache data that might not fit within the virtual address space.
Adrian McCarthy
+1  A: 

There are some obscure resources that Windows does not clean up when an app crashes or exits without explicitly releasing them, mostly because the OS doesn't know if they're important to leave around or not.

  1. Temporary files -- as others have mentioned.
  2. Globally registered WNDCLASSes ("No window classes registered by a DLL are unregistered when the DLL is unloaded. A DLL must explicitly unregister its classes when it is unloaded." MSDN) If your global window glass also has a class DC, then that DC will leak as well.
  3. Global ATOMs (a relatively limited resource).
  4. Window message IDs created with RegisterWindowMessage. These are designed to leak, since there's no UnregisterWindowMessage.
  5. Semaphores and Events aren't technically leaked, but when the owning application goes away without signalling them, then other processes can hang. This is not true for a Mutex. If the owning application goes away, other processes waiting on that Mutex are released.
  6. There may be some residual weirdness on Windows XP and earlier if you don't unregister a hot key before exiting. Other applications may be unable to register the same hot key.
  7. On Windows XP and earlier, it's not uncommon to have a zombie console window live on after a process crashes. (Specifically, a GUI application that also creates a console window.) It shows up on the task bar. All you can do is minimize, restore, or move the window.
  8. Buggy drivers can be aggravated by apps that don't explicitly release resources when they exit. Non-paged pool leaks are fairly common.
  9. Data copied to the clipboard. I guess that doesn't really count because it's owned by the OS at that point, not the application that put it there.
Adrian McCarthy