I am implementing an application using Qt C++
where I have used QSharedMemory
to restrict multiple instances of the application. Relevant code segment in main.cpp
is as follows,
QSharedMemory sharedMemory;
sharedMemory.setKey(SM_INSTANCE_KEY);
if (!sharedMemory.create(1))
{
QMessageBox::warning(0, "Console", "An instance of this application is already running!" );
exit(0); /* Exit, already a process is running */
}
On opening the Application, I can see that a shared memory has been created for my application. (shmid
7045192, size
1B)
So far so good. Problem arises when my application crashes for some reason. On crashing, the sharedMemory is not getting cleared, so I can't open the application anymore. When it crashes, attached application count becomes 0, but the shared memory does not get deleted. Relevant screen-shot is as follows
As per as my understanding, as the status of the shared memory is not marked as dest
like other shared memories, it is not getting deleted even when there is not any attached process.
So, my question is that is there any way of marking status of the Shared Memory as dest
?
Quoting
QSharedMemory
documentation:I've add the same issue on Linux few years ago, we solved the problem by doing those steps: