[interprocess] shared map of string to string?

I'm using boost.interprocess to build a map from string to string that can be shared between multiple processes, with all read/write access protected by a mutex. I'm having a hard time making it work. Here's a cut-down version of the declaration: template < typename KeyType, typename ValueType, typename StoredKeyType = KeyType, typename StoredValueType = ValueType > class SharedMap : private boost::noncopyable { public: SharedMap( const std::string & shmName, const std::size_t shmSize, const std::size_t initBucketCount = 10 ); ~SharedMap(); void setOneValue( const KeyType & key, const ValueType & value ); bool getOneValue( const KeyType & key, ValueType & value ); void clear(); private: typedef std::pair< const StoredKeyType, StoredValueType > StoredPairType; typedef boost::interprocess::managed_shared_memory ShmManagerType; typedef boost::interprocess::allocator< StoredPairType, ShmManagerType::segment_manager > ShmAllocatorType; typedef boost::interprocess::interprocess_mutex ShmMutexType; typedef boost::interprocess::scoped_lock< ShmMutexType > ShmScopedLock; typedef boost::unordered_map< StoredKeyType, StoredValueType, boost::hash< StoredKeyType >, std::equal_to< StoredKeyType >, ShmAllocatorType > ShmMapType; std::string m_sShmName; ShmManagerType m_manager; ShmMutexType * m_pMutex; ShmMapType * m_pMap; }; // end class SharedMap typedef SharedMap< /* KeyType = */ std::string, /* ValueType = */ std::string, /* StoredKeyType = */ boost::interprocess::string, /* StoredValueType = */ boost::interprocess::string > SharedStringMap; And some important bits of the implementation: #define SHARED_MAP( ret_type ) \ template < \ typename KeyType, \ typename ValueType, \ typename StoredKeyType, \ typename StoredValueType \ > \ ret_type \ SharedMap< \ KeyType, \ ValueType, \ StoredKeyType, \ StoredValueType \ > SHARED_MAP()::SharedMap( const std::string & shmName, const std::size_t shmSize, const std::size_t initBucketCount /* = 10 */ ) : m_sShmName( shmName ), m_manager( boost::interprocess::open_or_create, shmName.c_str(), shmSize ), m_pMutex( m_manager.find_or_construct< ShmMutexType >( "mutex" )() ), m_pMap( m_manager.find_or_construct< ShmMapType >( "map" )( initBucketCount, boost::hash< StoredKeyType >(), std::equal_to< StoredKeyType >(), m_manager.get_allocator< StoredPairType >() ) ) { } SHARED_MAP()::~SharedMap() { try { m_pMutex->unlock(); } catch ( ... ) { } } SHARED_MAP( void )::clear() { ShmScopedLock lock( *m_pMutex ); m_pMap->clear(); } It works fine the first time I run the program, but on the second attempt I get a SIGSEGV when I try to clear the map. My test loop is basically: create or find map for each rep clear map insert values done GDB tells me that this error is originating here: Program received signal SIGSEGV, Segmentation fault. __libc_free (mem=0x8143a48) at malloc.c:3709 3709 if (chunk_is_mmapped(p)) /* release mmapped memory. */ (gdb) bt #0 __libc_free (mem=0x8143a48) at malloc.c:3709 #1 0x001efd12 in operator delete (ptr=<value optimized out>) at ../../../../libstdc++-v3/libsupc++/del_op.cc:44 #2 0x08053d49 in __gnu_cxx::new_allocator<char>::deallocate (this=0xb7fa4334, __p=0x8143a48
) at /usr/lib/gcc/i686-redhat-linux/4.4.2/../../../../include/c++/4.4.2/ext/new_allocator.h:95 #3 0x0805e677 in boost::interprocess_container::containers_detail::basic_string_base
On Jan 19, 2010, at 19:50, Anthony Foiani wrote:
I'm using boost.interprocess to build a map from string to string that can be shared between multiple processes, with all read/write access protected by a mutex. I'm having a hard time making it work.
Thanks to some helpful folks on freenode #boost pointing out the obvious, I got it working. My main mental block was not realizing that boost::interprocess::basic_string is just another basic_string implementation; it's structured to work better in shared memory, but the default instantiation is just a plain string (no fancy shared memory bits). Thanks for the great library! Regards, Tony
participants (2)
-
Anthony Foiani
-
Tkil