Boost logo

Boost Users :

Subject: [Boost-users] problem with memcpy to shared memory
From: Cuneyt Taskiran (ctaskiran_at_[hidden])
Date: 2010-03-02 12:05:48


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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net