title: Shared memory ring buffer crashes
body: <p>I wrote a shared memory ringbuffer with QT and I turns out it works great in a single process with one consumer and one writer. If I try to read data from a second process I the first one which connected crashes with a seg fault. Maybe I'm overlooking something?</p>
  
  <p>The Header File</p>
  
  <pre><code>#ifndef SHAREDMEMORYRINGBUFFER_H
  #define SHAREDMEMORYRINGBUFFER_H
  
  #include &lt;QObject&gt;
  #include &lt;QSharedMemory&gt;
  
  class SharedMemoryRingBuffer : public QObject
  {
      Q_OBJECT
  public:
      struct SharedMemoryAttributes {
          int32_t readPosition;
          int32_t writePosition;
          int32_t size;
          int32_t* data;
      };
      explicit SharedMemoryRingBuffer(QString sharedMemoryName, int32_t size, QObject *parent = 0);
      ~SharedMemoryRingBuffer();
      int write(int32_t frame);
      int32_t read();
  
      int32_t length();
      int32_t readPosition();
      int32_t writePosition();
  
      int avaibleSize();
      int isEmpty();
      void empty();
      int isFull();
  
  
  
  private:
      SharedMemoryAttributes *_attributes;
      int32_t _writePosition;
      int32_t _readPosition;
      int32_t _length;
      QSharedMemory *_sharedMemory;
      int _headSize;
  signals:
      void readFrame(QString name);
  public slots:
  };
  
  #endif // SHAREDMEMORYRINGBUFFER_H
  </code></pre>
  
  <p>And the CPP File:</p>
  
  <pre><code>#include "sharedmemoryringbuffer.h"
  #include &lt;QDebug&gt;
  #define DEBUGGINGNAME "[SharedMemoryRingbuffer]"
  
  SharedMemoryRingBuffer::SharedMemoryRingBuffer(QString sharedMemoryName, int32_t size, QObject *parent) : QObject(parent)
  {
      int sizeOfData = sizeof(int32_t) * (size);
      int sizeOfHeader = sizeof(SharedMemoryAttributes);
      _sharedMemory = new QSharedMemory(sharedMemoryName);
  
      if (!_sharedMemory-&gt;attach()) {
          if (!_sharedMemory-&gt;create(sizeOfData+sizeOfHeader, QSharedMemory::ReadWrite )) {
              qDebug() &lt;&lt; DEBUGGINGNAME &lt;&lt; "Could not create shared memory object, aborting ...";
              qDebug() &lt;&lt; DEBUGGINGNAME &lt;&lt; _sharedMemory-&gt;errorString();
              //TODO: exit call
              return;
          }
      }
  
      _sharedMemory-&gt;lock();
      _attributes = reinterpret_cast&lt;SharedMemoryAttributes*&gt;(_sharedMemory-&gt;data());
      _attributes-&gt;readPosition = 0;
      _attributes-&gt;writePosition = 0;
      _attributes-&gt;size = size + 1;
      _attributes-&gt;data = (int32_t*) _sharedMemory-&gt;data() + sizeOfHeader;
      _sharedMemory-&gt;unlock();
  }
  
  SharedMemoryRingBuffer::~SharedMemoryRingBuffer()
  {
      _sharedMemory-&gt;detach();
      delete _sharedMemory;
  }
  
  int SharedMemoryRingBuffer::write(int32_t frame)
  {
      _sharedMemory-&gt;lock();
      if ( !isFull() ) {
          _attributes-&gt;data[writePosition()] = frame;
          if (writePosition() + 1 &gt;= _attributes-&gt;size) _attributes-&gt;writePosition = 0;
          else _attributes-&gt;writePosition += 1;
          _sharedMemory-&gt;unlock();
          return 1;
      }
      _sharedMemory-&gt;unlock();
      return 0;
  }
  
  
  /**
   * @brief SharedMemoryRingBuffer::read
   * @return
   * if the buffer is empty this functions returns the last readable value
   *
   */
  int32_t SharedMemoryRingBuffer::read()
  {
      _sharedMemory-&gt;lock();
      int32_t frame = _attributes-&gt;data[readPosition()];
      if ( readPosition() != writePosition() ) {
          if (readPosition() + 1 &gt;= _attributes-&gt;size ) _attributes-&gt;readPosition = 0;
          else _attributes-&gt;readPosition += 1;
          emit readFrame(_sharedMemory-&gt;key());
      }
      _sharedMemory-&gt;unlock();
      return frame;
  }
  
  int32_t SharedMemoryRingBuffer::length()
  {
      if(readPosition() &lt;= writePosition()) {
          return writePosition() - readPosition();
      } else {
          return readPosition() - writePosition();
      }
  }
  
  int32_t SharedMemoryRingBuffer::readPosition()
  {
      return _attributes-&gt;readPosition;
  }
  
  int32_t SharedMemoryRingBuffer::writePosition()
  {
      return _attributes-&gt;writePosition;
  }
  
  int SharedMemoryRingBuffer::avaibleSize()
  {
      return -1;
  }
  
  int SharedMemoryRingBuffer::isEmpty()
  {
      if ( readPosition() == writePosition() ) return 1;
      else return 0;
  }
  
  void SharedMemoryRingBuffer::empty()
  {
  
  }
  
  int SharedMemoryRingBuffer::isFull()
  {
      if ((writePosition() + 1) % _attributes-&gt;size == readPosition()) return 1;
      else return 0;
  }
  </code></pre>