C++ shared memory leak, how to clear shared memory?

来源:互联网 发布:戒断反应 戒烟 知乎 编辑:程序博客网 时间:2024/05/17 22:59


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/bashME=$(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_Sdo    ipcrm -s $iddoneecho "Clearing Shared Memory"for id in $IPCS_M do    ipcrm -m $iddoneecho "Clearing Message Queues"for id in $IPCS_Qdo    ipcrm -q $iddone
0 0
原创粉丝点击