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);
QSharedMemory shared(ApplicationConstants::
if( !shared.create( 512, QSharedMemory::ReadWrite) )
{
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() .