views:

69

answers:

3

When a program with some theards, mutexes, shared data, file handles crash because of too much memory allocation, which all resources are freed. How do you recover?

+1  A: 

You recover by checking the results of resource acquisition functions and not allowing unchecked errors to occur in the first place.

Ignacio Vazquez-Abrams
In the real world though, bugs happen.
leeeroy
Try/catch on memory allocation can help you safely shut down though.Unless OOM killer gets you first.
Xorlev
malloc never fails on Linux. Linux just kills the app when it tries to use the nonexistent memory. A try/catch will not help with this.
Justin Smith
+1  A: 

If you mean, how do you go back and free up the resources that were allocated by the now-crashed process, well, you don't have to.

When the process exit(2)'s or dies by a signal all of the OS-allocated resources will be retrieved. This is the kernel's job.

DigitalRoss
But the process is not doing a clean exit- isnt that why the os did not release it in the first place?
Swapna
It doesn't matter whether the exit occurs as a result of a system call or a signal, it will execute the same kernel code and do the same cleanup. That's the kernel's job and if it doesn't happen it's called a leak, it's about as serious as a kernel bug can be, and it's cause to withdraw a release, post security alerts, and generally launch into all kinds of emergency update hysteria.
DigitalRoss
+1  A: 

All resources that belongs to the process are cleaned up.

The only exceptions would be the sysv shared memory/message queues/semaphores - which although might have been created by the process are not owned by it.

nos