How to release resources on process stop/exit?

I have a process layed out as follows:

open / alloc stuff message parsing in never-ending loop close / free stuff

Obviously, the close / free stuff is never executed.

My questions:

  1. how can I detect and act upon a process stop/exit (according to docs SIGSTOP / SIGEXIT can’t be caught)?

  2. is it at all needed to close / free stuff on process exit/stop - i.e. is it done by the OS?

Jacob Dall