Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Share class object between processes through shared memory
Forum Updated to NodeBB v4.3 + New Features

Share class object between processes through shared memory

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ADedras
    wrote on last edited by
    #1

    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.

    aha_1980A 1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      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.

      If you meet the AI on the road, kill it.

      A 1 Reply Last reply
      1
      • A ADedras

        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.

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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

        Qt has to stay free or it will die.

        A 1 Reply Last reply
        5
        • Kent-DorfmanK Kent-Dorfman

          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.

          A Offline
          A Offline
          ADedras
          wrote on last edited by
          #4

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

          Kent-DorfmanK 1 Reply Last reply
          0
          • aha_1980A aha_1980

            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

            A Offline
            A Offline
            ADedras
            wrote on last edited by
            #5

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

            1 Reply Last reply
            1
            • A ADedras

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

              Kent-DorfmanK Offline
              Kent-DorfmanK Offline
              Kent-Dorfman
              wrote on last edited by
              #6

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

              If you meet the AI on the road, kill it.

              1 Reply Last reply
              2

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved