http://stackoverflow.com/questions/7773276/c-shared-memory-leak-how-to-clear-shared-memory
You could always run a script after termination of your program to manually clear the shared memory, semaphores, etc. on your system (mine is a Mac Pro running 10.8). I have inserted a script I use for this when running programs that use QSharedMemory, and use it when the program quits unexpectedly and leaves the shared memory instances "hanging".
Keep in mind this will remove all shared memory instances associated with your user name. If you have multiple programs running and using shared memory instances, you should either wait until every program is done, or adjust the script as needed to only delete the shared memory instances that were created by your program.
#!/bin/bash
ME=$(whoami)
IPCS_S=$(ipcs -s | grep $ME | sed "s/ / /g" | cut -f2 -d " ")
IPCS_M=$(ipcs -m | grep $ME | sed "s/ / /g" | cut -f2 -d " ")
IPCS_Q=$(ipcs -q | grep $ME | sed "s/ / /g" | cut -f2 -d " ")
echo "Clearing Semaphores"
for id in $IPCS_S
do
ipcrm -s $id
done
echo "Clearing Shared Memory"
for id in $IPCS_M
do
ipcrm -m $id
done
echo "Clearing Message Queues"
for id in $IPCS_Q
do
ipcrm -q $id
done