Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Share class object between processes through shared memory



  • 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.



  • 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.


  • Lifetime Qt Champion

    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



  • @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 ?



  • @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.



  • @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.


Log in to reply