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:
void MemWriter::Write()
{
int* data = (int*)mem->data(); //'mem' is QSharedMemory, created elsewhere
for (int i=min; i<max+1; ++i)
data[i-min] = i;
}
void MemReader::Read()
{
int* data = (int*)(mem->data()); //'mem' is QSharedMemory, created elsewhere
for (int i=0; i<range; ++i)
qDebug() << data[i];
}
void MemWriter::Write()
{
int* data = (int*)mem->data(); //'mem' is QSharedMemory, created elsewhere
for (int i=min; i<max+1; ++i)
data[i-min] = i;
}
void MemReader::Read()
{
int* data = (int*)(mem->data()); //'mem' is QSharedMemory, created elsewhere
for (int i=0; i<range; ++i)
qDebug() << data[i];
}
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:
void MemWriter::Write()
{
vector<int>* vec = reinterpret_cast< vector<int>* >(mem->data()); //'mem' is QSharedMemory, created elsewhere
for (int i=min; i<max+1; ++i)
vec->push_back(i);
}
void MemReader::Read()
{
vector<int>* data = reinterpret_cast< vector<int>* >(mem->data()); //'mem' is QSharedMemory, created elsewhere
for (vector<int>::iterator it=data->begin(); it!=data->end(); ++it)
qDebug() << *it;
}
void MemWriter::Write()
{
vector<int>* vec = reinterpret_cast< vector<int>* >(mem->data()); //'mem' is QSharedMemory, created elsewhere
for (int i=min; i<max+1; ++i)
vec->push_back(i);
}
void MemReader::Read()
{
vector<int>* data = reinterpret_cast< vector<int>* >(mem->data()); //'mem' is QSharedMemory, created elsewhere
for (vector<int>::iterator it=data->begin(); it!=data->end(); ++it)
qDebug() << *it;
}
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
Bookmarks