Detach posix shared memory correctly
* shmget() fails after shmctl() is called with IPC_RMID. This patch removes shmget() in releaseSharedMemory so that a client can detach a shared memory correctly after a server removed it. * A server should also detach a shared memory.
This commit is contained in:
@@ -19,10 +19,12 @@
|
||||
struct PosixSharedMemoryInteralData
|
||||
{
|
||||
bool m_createdSharedMemory;
|
||||
int m_sharedMemoryId;
|
||||
void* m_sharedMemoryPtr;
|
||||
|
||||
PosixSharedMemoryInteralData()
|
||||
:m_createdSharedMemory(false),
|
||||
m_sharedMemoryId(-1),
|
||||
m_sharedMemoryPtr(0)
|
||||
{
|
||||
}
|
||||
@@ -66,6 +68,7 @@ void* PosixSharedMemory::allocateSharedMemory(int key, int size, bool allowCr
|
||||
} else
|
||||
{
|
||||
m_internalData->m_createdSharedMemory = allowCreation;
|
||||
m_internalData->m_sharedMemoryId = id;
|
||||
m_internalData->m_sharedMemoryPtr = result.ptr;
|
||||
return result.ptr;
|
||||
}
|
||||
@@ -79,16 +82,14 @@ void* PosixSharedMemory::allocateSharedMemory(int key, int size, bool allowCr
|
||||
void PosixSharedMemory::releaseSharedMemory(int key, int size)
|
||||
{
|
||||
#ifdef TEST_SHARED_MEMORY
|
||||
int flags = 0666;
|
||||
int id = shmget((key_t) key, (size_t) size,flags);
|
||||
if (id < 0)
|
||||
if (m_internalData->m_sharedMemoryId < 0)
|
||||
{
|
||||
b3Error("PosixSharedMemory::releaseSharedMemory: shmget error");
|
||||
b3Error("PosixSharedMemory::releaseSharedMemory: shared memory id is not set");
|
||||
} else
|
||||
{
|
||||
if (m_internalData->m_createdSharedMemory)
|
||||
{
|
||||
int result = shmctl(id,IPC_RMID,0);
|
||||
int result = shmctl(m_internalData->m_sharedMemoryId,IPC_RMID,0);
|
||||
if (result == -1)
|
||||
{
|
||||
b3Error("PosixSharedMemory::releaseSharedMemory: shmat returned -1");
|
||||
@@ -97,8 +98,9 @@ void PosixSharedMemory::releaseSharedMemory(int key, int size)
|
||||
b3Printf("PosixSharedMemory::releaseSharedMemory removed shared memory");
|
||||
}
|
||||
m_internalData->m_createdSharedMemory = false;
|
||||
m_internalData->m_sharedMemoryPtr = 0;
|
||||
} else
|
||||
m_internalData->m_sharedMemoryId = -1;
|
||||
}
|
||||
if (m_internalData->m_sharedMemoryPtr)
|
||||
{
|
||||
shmdt(m_internalData->m_sharedMemoryPtr);
|
||||
m_internalData->m_sharedMemoryPtr = 0;
|
||||
|
||||
Reference in New Issue
Block a user