I would look at mmap as a replacement for shm; shm causes quite a few problems related to what you're saying, namely that the segment won't be freed unless one process flags it, even if no existing process holds reference to it. You really shouldn't have to ask that question; it should happen automatically. If it doesn't then that sounds like a majorly-flawed system to me.
PS As with anything that has external cleanup, though, you should have a signal handler for every possible signal that can cause termination that will unlock the memory. The exception is SIGKILL, but there's nothing you can do about that. That's why SIGKILL is highly discouraged.
Last edited by ta0kira; 12-06-2008 at 04:08 PM.