problem with memcpy to shared memory
Hi, I want to create a buffer in shared memory and have modified the example code provided in Boost.Interprocess to get the following class: template<typename ItemType> class SharedMemoryBufferMock { private: ItemType* m_data; // array of ItemType items ItemType* m_read_ptr; // points to next buffer location to be read from ItemType* m_write_ptr; // points to next buffer location to be written to std::string m_segmentName; // unique name for the memory segment that holds the buffer object std::string m_objectName; // unique name for the buffer object unsigned long m_nItems; unsigned long m_capacity; public: SharedMemoryBufferMock( unsigned long maxItemCount ); virtual ~SharedMemoryBufferMock(); unsigned long capacity() { return m_capacity; } unsigned long size() { return m_nItems; } bool isFull() { return m_nItems == m_capacity; } bool isEmpty() { return m_nItems == 0; } ItemType* read(); void write( ItemType* item_ptr); }; /* * Implementation of the templated methods. */ template<typename ItemType>SharedMemoryBufferMock<ItemType>::SharedMemoryBufferMock( unsigned long maxItemCount ) { if( maxItemCount == 0 ) { std::cerr << "Buffer cannot be created with a capacity of zero!" << std::endl; exit(1); } // Create unique name for memory segment using PID of current process. std::stringstream pid_str; pid_str << "BufferMemorySegment_" << getpid(); m_segmentName = pid_str.str(); pid_str.str(""); pid_str << "BufferMemoryObject_" << getpid(); m_objectName = pid_str.str(); boost::interprocess::shared_memory_object::remove(m_segmentName.c_str()); m_capacity = maxItemCount; // Create a new segment with given name and size boost::interprocess::managed_shared_memory segment( boost::interprocess::create_only, m_segmentName.c_str(), //memSize 65536 ); // Create the buffer data object with specified item capacity. m_data = segment.construct<ItemType> ( m_objectName.c_str() ) // name of the object [maxItemCount] // number of items (ItemType()); // ctor for elements // Initialize read and write locations. m_write_ptr = m_read_ptr = m_data; } template<typename ItemType> void SharedMemoryBufferMock<ItemType>::write( ItemType* item_ptr ) { if( isFull() ) { std::cerr << "Cannot write to buffer, it's full. Read one or more elements first!" << std::endl; exit(1); } std::memcpy( m_write_ptr, item_ptr, sizeof(ItemType) ); m_write_ptr++; m_nItems++; } When I test the code using the following: SharedMemoryBufferMock<int> sharedBuf(100); for( int i = 0; i < 10; i++ ) { cout << "writing " << i << endl; sharedBuf.write( &i ); } I get a segmentation fault at the memcpy step. I've been at this for a couple of hours now and cannot see what I'm doing wrong. Any comments would be greatly appreciated. Thanks, C
Sorry, forgot to provide gdb output in my message:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x00000001000990e8
0x00007fffffe007c5 in __memcpy ()
The address 0x00000001000990e8 is the start address of the buffer,
i.e. the initial value of m_write_ptr.
The output of where is:
(gdb) where
#0 0x00007fffffe007c5 in __memcpy ()
#1 0x0000000100002e64 in SharedMemoryBufferMock<int>::write
(this=0x7fff5fbff7d0, item_ptr=0x7fff5fbff828) at
SharedMemoryBufferMock.hpp:93
#2 0x0000000100000c7e in main (argc=1, argv=0x7fff5fbff868) at ../main.cpp:27
I am running this on a MAC with snow Leopard, could that have
something to do with this?
Thanks,
C
On Tue, Mar 2, 2010 at 11:05 AM, Cuneyt Taskiran
Hi,
I want to create a buffer in shared memory and have modified the example code provided in Boost.Interprocess to get the following class:
template<typename ItemType> class SharedMemoryBufferMock { private: ItemType* m_data; // array of ItemType items ItemType* m_read_ptr; // points to next buffer location to be read from ItemType* m_write_ptr; // points to next buffer location to be written to std::string m_segmentName; // unique name for the memory segment that holds the buffer object std::string m_objectName; // unique name for the buffer object unsigned long m_nItems; unsigned long m_capacity; public: SharedMemoryBufferMock( unsigned long maxItemCount ); virtual ~SharedMemoryBufferMock();
unsigned long capacity() { return m_capacity; } unsigned long size() { return m_nItems; } bool isFull() { return m_nItems == m_capacity; } bool isEmpty() { return m_nItems == 0; }
ItemType* read(); void write( ItemType* item_ptr); };
/* * Implementation of the templated methods. */ template<typename ItemType>SharedMemoryBufferMock<ItemType>::SharedMemoryBufferMock( unsigned long maxItemCount ) { if( maxItemCount == 0 ) { std::cerr << "Buffer cannot be created with a capacity of zero!" << std::endl; exit(1); }
// Create unique name for memory segment using PID of current process. std::stringstream pid_str; pid_str << "BufferMemorySegment_" << getpid(); m_segmentName = pid_str.str(); pid_str.str(""); pid_str << "BufferMemoryObject_" << getpid(); m_objectName = pid_str.str();
boost::interprocess::shared_memory_object::remove(m_segmentName.c_str());
m_capacity = maxItemCount;
// Create a new segment with given name and size boost::interprocess::managed_shared_memory segment( boost::interprocess::create_only, m_segmentName.c_str(), //memSize 65536 );
// Create the buffer data object with specified item capacity. m_data = segment.construct<ItemType> ( m_objectName.c_str() ) // name of the object [maxItemCount] // number of items (ItemType()); // ctor for elements
// Initialize read and write locations. m_write_ptr = m_read_ptr = m_data; }
template<typename ItemType> void SharedMemoryBufferMock<ItemType>::write( ItemType* item_ptr ) { if( isFull() ) { std::cerr << "Cannot write to buffer, it's full. Read one or more elements first!" << std::endl; exit(1); } std::memcpy( m_write_ptr, item_ptr, sizeof(ItemType) ); m_write_ptr++; m_nItems++; }
When I test the code using the following: SharedMemoryBufferMock<int> sharedBuf(100); for( int i = 0; i < 10; i++ ) { cout << "writing " << i << endl; sharedBuf.write( &i ); }
I get a segmentation fault at the memcpy step. I've been at this for a couple of hours now and cannot see what I'm doing wrong. Any comments would be greatly appreciated.
Thanks,
C
participants (1)
-
Cuneyt Taskiran