Hello,

I can get QSharedMemory to work just fine with arrays of ints, but not with a vector, so I'm wondering if anyone knows how to do the latter. Obviously it's simple enough to convert a vector of ints into an array of ints and share that, but I want to be able to share complex C++ classes so I figured I would just try to get it to work with a vector first. Anyway, here's what works:

Qt Code:
  1. void MemWriter::Write()
  2. {
  3. int* data = (int*)mem->data(); //'mem' is QSharedMemory, created elsewhere
  4. for (int i=min; i<max+1; ++i)
  5. data[i-min] = i;
  6. }
  7.  
  8. void MemReader::Read()
  9. {
  10. int* data = (int*)(mem->data()); //'mem' is QSharedMemory, created elsewhere
  11. for (int i=0; i<range; ++i)
  12. qDebug() << data[i];
  13. }
To copy to clipboard, switch view to plain text mode 

I.e., the the integers 0-9 are just written into the shared memory block and then read out again. MemWriter::Write() and MemReader::Read() are in different processes.

Here's what doesn't work:

Qt Code:
  1. void MemWriter::Write()
  2. {
  3. vector<int>* vec = reinterpret_cast< vector<int>* >(mem->data()); //'mem' is QSharedMemory, created elsewhere
  4. for (int i=min; i<max+1; ++i)
  5. vec->push_back(i);
  6. }
  7.  
  8. void MemReader::Read()
  9. {
  10. vector<int>* data = reinterpret_cast< vector<int>* >(mem->data()); //'mem' is QSharedMemory, created elsewhere
  11. for (vector<int>::iterator it=data->begin(); it!=data->end(); ++it)
  12. qDebug() << *it;
  13. }
To copy to clipboard, switch view to plain text mode 

This crashes when Read() is called. I've tried a few different variations on this, such as static_cast instead of reinterpret_cast--in that case it crashes/produces undefined behavior during Write(). Am I stuck with C int/double/etc arrays?

Matt