From 9442791cf1791cbdba916452ba391f5b1c15b38f Mon Sep 17 00:00:00 2001 From: erwin coumans Date: Thu, 29 Oct 2015 11:18:26 -0700 Subject: [PATCH] Fix PhysicsServer connection issue, in cases where shared memory was not properly shut down. --- examples/SharedMemory/PhysicsServer.cpp | 51 +++++++++++++++---------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/examples/SharedMemory/PhysicsServer.cpp b/examples/SharedMemory/PhysicsServer.cpp index b93ca04b3..9dfc8e413 100644 --- a/examples/SharedMemory/PhysicsServer.cpp +++ b/examples/SharedMemory/PhysicsServer.cpp @@ -460,34 +460,43 @@ bool PhysicsServerSharedMemory::connectSharedMemory( struct GUIHelperInterface* } - m_data->m_testBlock1 = (SharedMemoryBlock*)m_data->m_sharedMemory->allocateSharedMemory(m_data->m_sharedMemoryKey, SHARED_MEMORY_SIZE,allowCreation); - if (m_data->m_testBlock1) - { - int magicId =m_data->m_testBlock1->m_magicId; - if (m_data->m_verboseOutput) + int counter = 0; + do + { + + m_data->m_testBlock1 = (SharedMemoryBlock*)m_data->m_sharedMemory->allocateSharedMemory(m_data->m_sharedMemoryKey, SHARED_MEMORY_SIZE,allowCreation); + if (m_data->m_testBlock1) { - b3Printf("magicId = %d\n", magicId); - } - - if (m_data->m_testBlock1->m_magicId !=SHARED_MEMORY_MAGIC_NUMBER) - { - InitSharedMemoryBlock(m_data->m_testBlock1); + int magicId =m_data->m_testBlock1->m_magicId; if (m_data->m_verboseOutput) { - b3Printf("Created and initialized shared memory block\n"); + b3Printf("magicId = %d\n", magicId); } - m_data->m_isConnected = true; - } else + + if (m_data->m_testBlock1->m_magicId !=SHARED_MEMORY_MAGIC_NUMBER) + { + InitSharedMemoryBlock(m_data->m_testBlock1); + if (m_data->m_verboseOutput) + { + b3Printf("Created and initialized shared memory block\n"); + } + m_data->m_isConnected = true; + } else + { + m_data->m_sharedMemory->releaseSharedMemory(m_data->m_sharedMemoryKey, SHARED_MEMORY_SIZE); + m_data->m_testBlock1 = 0; + m_data->m_isConnected = false; + } + } else { - b3Error("Server cannot connect to existing shared memory, disconnecting shared memory.\n"); - m_data->m_sharedMemory->releaseSharedMemory(m_data->m_sharedMemoryKey, SHARED_MEMORY_SIZE); - m_data->m_testBlock1 = 0; - m_data->m_isConnected = false; + b3Error("Cannot connect to shared memory"); + m_data->m_isConnected = false; } - } else + } while (counter++ < 10 && !m_data->m_isConnected); + + if (!m_data->m_isConnected) { - b3Error("Cannot connect to shared memory"); - m_data->m_isConnected = false; + b3Error("Server cannot connect to shared memory.\n"); } return m_data->m_isConnected; }