PDA

View Full Version : Create new QsharedMemory after crash



dashesy
30th June 2012, 01:06
If application owning the QSharedMemory crashes, it will not detach so next time create will fail.
From the document:
QSharedMemory "owns" the shared memory segment. When the last thread or process that has an instance of QSharedMemory attached to a particular shared memory segment detaches from the segment by destroying its instance of QSharedMemory, the Unix kernel release the shared memory segment. But if that last thread or process crashes without running the QSharedMemory destructor, the shared memory segment survives the crash.


My question is this: before I call create, if I try to atatch and detach will it detect that this is the last process and thus release the segment? If not, is there any Qt way to release the shared memory without using ipcrm?

Here is a possible code snippet:



QSharedMemory * hnd = NULL;
QSharedMemory * pHnd = new QSharedMemory(strKey);
if (pHnd)
{
// Try to attach and detach to handle crashed segments
if (pHnd->attach(QSharedMemory::ReadOnly))
pHnd->detach();
// Now create brand new segment
if (pHnd->create(size))
hnd = pHnd;
else
delete pHnd;
}
return hnd; // Null if unsuccessful


Thanks

wysota
30th June 2012, 08:01
Why don't you just try and see?