Share class object between processes through shared memory
-
wrote on 26 Apr 2019, 11:28 last edited by
Hi,
I am trying to exchange data between processes thanks to a shared memory. Everything works fine for a QString exchange. I am trying now to exchange my object "SMStructure" :
#include "qstring.h"
class SMStructure
{
public:
QString dataString;
int dataInt;};
In my writing I have :
SMStructure *structure = new SMStructure;
structure->dataString = "Hello";
structure->dataInt = 1;
SMStructure bufferToSM = (SMStructure)SharedMemory.data();
mempcpy(bufferToSM, structure, sizeof(structure));And in my reading :
QTextStream streamOut(stdout);
SMStructure *structure = new SMStructure;
structure = (SMStructure)SharedMemory.constData();
streamOut << "DataRead : " << Data << endl;But when I am debugging, structure can retrieve the address of the shared memory when calling the constdata() method but the dataString value is unattainable.
Thanks for the help.
-
wrote on 26 Apr 2019, 14:32 last edited by
It's much more complicated than simply having multiple processes see that same region of data. Avoid shared memory IPC unless you know what you are doing. If the IPC is a simple point-to-point communication then use a FIFO or message queue instead.
But the greater issue with shared memory IPC us that you MUST semaphore lock the region to keep multiple processes from simultaneously stomping on it.
-
Hi,
I am trying to exchange data between processes thanks to a shared memory. Everything works fine for a QString exchange. I am trying now to exchange my object "SMStructure" :
#include "qstring.h"
class SMStructure
{
public:
QString dataString;
int dataInt;};
In my writing I have :
SMStructure *structure = new SMStructure;
structure->dataString = "Hello";
structure->dataInt = 1;
SMStructure bufferToSM = (SMStructure)SharedMemory.data();
mempcpy(bufferToSM, structure, sizeof(structure));And in my reading :
QTextStream streamOut(stdout);
SMStructure *structure = new SMStructure;
structure = (SMStructure)SharedMemory.constData();
streamOut << "DataRead : " << Data << endl;But when I am debugging, structure can retrieve the address of the shared memory when calling the constdata() method but the dataString value is unattainable.
Thanks for the help.
Hi @ADedras
class SMStructure
{
public:
QString dataString;
int dataInt;
};Copying this structure simply cannot work.
You can exchange the int that way, but not the QString, because that is effectively just a pointer to heap memory. In the other program, that pointer is just invalid.
Regards
-
It's much more complicated than simply having multiple processes see that same region of data. Avoid shared memory IPC unless you know what you are doing. If the IPC is a simple point-to-point communication then use a FIFO or message queue instead.
But the greater issue with shared memory IPC us that you MUST semaphore lock the region to keep multiple processes from simultaneously stomping on it.
wrote on 29 Apr 2019, 07:55 last edited by@Kent-Dorfman First, thank you for taking time to answer my question and for the advices.
We decided to go with the shared memory because it's the most suited solution for our need.I looked about the semaphore lock and I was thinking about only a single process can write the shared memory and every other process can read or not the shared memory which make the semaphore lock useless from my point of view. Am I wrong ?
-
Hi @ADedras
class SMStructure
{
public:
QString dataString;
int dataInt;
};Copying this structure simply cannot work.
You can exchange the int that way, but not the QString, because that is effectively just a pointer to heap memory. In the other program, that pointer is just invalid.
Regards
wrote on 29 Apr 2019, 09:13 last edited by@aha_1980 Thanks for the answer.
You are right. That was the problem there. I couldn't pass that way.
I also didn't use a QString type because the size wasn't defined when I was initializing my shared memory.
I made the following changes :
class SMStructure
{
public:
char dataString[256];
int dataInt;
};Writing :
SMStructure bufferToSM = (SMStructure)SharedMemory.data();Reading :
SMStructure bufferFromSM = (SMStructure)SharedMemory.data();
mempcpy(Structure, bufferFromSM, sizeof(Structure);Everything works fine for the moment.
-
@Kent-Dorfman First, thank you for taking time to answer my question and for the advices.
We decided to go with the shared memory because it's the most suited solution for our need.I looked about the semaphore lock and I was thinking about only a single process can write the shared memory and every other process can read or not the shared memory which make the semaphore lock useless from my point of view. Am I wrong ?
wrote on 29 Apr 2019, 18:25 last edited by@ADedras said in Share class object between processes through shared memory:
looked about the semaphore lock and I was thinking about only a single process can write the shared memory and every other process can read or not the shared memory which make the semaphore lock useless from my point of view. Am I wrong ?
Yes...you are wrong. Unless your data is all atomic read operations then readers may/will read invalid data at some point...have fun tracking that one down six months from now.
As I said previously, shared memory IPC has pitfalls. It is often the most flexible, but also the most dangerous to shoot yourself in the foot.
1/6