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

I am using Qt and trying to create one instance application using this solution on Linux (ubuntu) . The problem is that if the application unexpectedly terminates (for example, an error or the user kills it), the shared memory remains attached, and no other process can create it again. Recall from the QSharedMemory document:

Unix: QSharedMemory owns a shared memory segment. When the last thread or process that has a QSharedMemory instance attached to a specific shared memory segment is separated from the segment by destroying its QSharedMemory instance, the Unix kernel releases the shared memory segment. But if this last thread or process crashes without starting the QSharedMemory destructor, the shared memory segment is in disaster.

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // Ensure single instanse of Cevirgec application
    QSharedMemory shared(ApplicationConstants::

    if( !shared.create( 512, QSharedMemory::ReadWrite) )
    {
      // QMessageBox msgBox;
      QMessageBox::critical(0, QObject::tr("application is already running!"), QObject::tr("application is already running!"), QMessageBox::Ok, QMessageBox::Ok);
      qCritical() << "application is already running!";

      exit(0);
    }
    else {
        qDebug() << "application staring...";
    }
    return a.exec(); 
}

What solutions can you offer here? How can I assure that shared memory is cleared (or some verb used in general) after the process is complete. I need something like finallyin java around the main function: /

EDIT: (solution)

, QSharedMemory SIGSEGV, sharedMemory.detach() .

+5
3

, , QSharedMemory.

+4

, segfault, . UNIX/Linux. , , .

EDIT:

sem_close

execve (2).

, , linux, - , - ssh X - - , . confisunig. , X-.

, , , pid. , . /proc/ [pid]/exe , .

+1

script , , .. ( Mac Pro 10.8). script, , QSharedMemory, , "".

Remember that this will delete all shared memory instances associated with your username. If you have several running programs and using shared memory instances, you must either wait for each program to finish, or adjust the script as necessary, only to delete the shared memory instances 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
+1
source

All Articles