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. Sending a struct via QShareMemory
Forum Updated to NodeBB v4.3 + New Features

Sending a struct via QShareMemory

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 3.4k 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.
  • R Offline
    R Offline
    r_spb
    wrote on last edited by
    #1

    Hi! I have a struct:
    struct control_data{
    int column_number;
    QString cell;
    };

    I need to send it from one thread to another.
    Sending thread:
    shmem.setKey("shmem");
    if (shmem.isAttached()) shmem.detach();
    if (!shmem.create(512)){
    qDebug() << "error creating QSharedMemory segment";
    if(shmem.error()) qDebug() << shmem.errorString();
    }
    ....
    QBuffer buffer;
    buffer.open( QBuffer::ReadWrite );
    QDataStream out( &buffer );
    control_data data;
    data.cell = "example";
    data.column_number = 2;
    out << data.cell;
    out << data.column_number;
    int size = buffer.size();
    shmem.lock();
    char *to = (char*)shmem.data();
    const char *from = buffer.data().data();
    memcpy(to, from,qMin( shmem.size(), size ));
    shmem.unlock();
    And recieving thread:
    shmem.setKey("shmem");
    ....
    control_data input;
    QBuffer buffer;
    buffer.open( QBuffer::ReadWrite );
    QDataStream in( &buffer );
    if (!shmem.attach()) {
    qDebug() << "MainWindow: Unable to attach to shared memory segment";
    return;
    }
    shmem.lock();
    buffer.setData((char*)shmem.constData(), shmem.size());
    buffer.open(QBuffer::ReadOnly);
    in >> input.cell;
    in >> input.column_number;
    shmem.unlock();
    shmem.detach();
    qDebug() << input.cell;
    qDebug() << input.column_number;

    So in this example instead of reading from shared memory int 2 and string "example" i read zero and empty string. What am i doing wrong?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mue22
      wrote on last edited by Mue22
      #2

      hi,
      I suggest that send the data of structure, not string &int.

      control_data data;
      data.cell = "example";
      data.column_number = 2;
      struct control_data *to = (control_data *)shmem.data();
      struct control_data *from =data;
      memcpy(to, from,sizeof(control_data));

      ->like this...

      i can&#x27;t use english well...

      R 1 Reply Last reply
      0
      • M Mue22

        hi,
        I suggest that send the data of structure, not string &int.

        control_data data;
        data.cell = "example";
        data.column_number = 2;
        struct control_data *to = (control_data *)shmem.data();
        struct control_data *from =data;
        memcpy(to, from,sizeof(control_data));

        ->like this...

        R Offline
        R Offline
        r_spb
        wrote on last edited by r_spb
        #3

        @Mue22 That doesn't work. Programm crashes.

        M 1 Reply Last reply
        0
        • R r_spb

          @Mue22 That doesn't work. Programm crashes.

          M Offline
          M Offline
          Mue22
          wrote on last edited by Mue22
          #4

          @r_spb

          umm...sorry. I couldn't test it...I can't use the computer now....

          Can you try to make struct in pointer? I missed that.

          struct control_data *data;
          data->....
          data->.....

          ->like this.

          i can&#x27;t use english well...

          R 1 Reply Last reply
          0
          • M Mue22

            @r_spb

            umm...sorry. I couldn't test it...I can't use the computer now....

            Can you try to make struct in pointer? I missed that.

            struct control_data *data;
            data->....
            data->.....

            ->like this.

            R Offline
            R Offline
            r_spb
            wrote on last edited by
            #5

            @Mue22 already made it, result is the same.
            On stackoverflow people told me that i have to serialize struct to a Byte array. The first post was near the thruth.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mue22
              wrote on last edited by Mue22
              #6

              SHM.setKey("TEST11");
              SHM.attach();
              SHM.create(sizeof(shm_BlockData));
              SHM.lock();

              shm_BlockData *from=new shm_BlockData;
              from->....;
              from->...;
              shm_BlockData to ;
              *
              to=(shm_BlockData
              )SHM.data();
              memcpy(to,from,sizeof(shm_BlockData));

              sorry, i find the code in my project....

              Maybe, the problem occured there ,

              struct control_data *data; -> struct control_data *data= new control_data;

              i miss allocate ...

              sorry, i don't know byte array method.. another programmer will help you!
              i use that(struct method) in my project. i hope to help you using my reply...:(

              i can&#x27;t use english well...

              R 1 Reply Last reply
              0
              • M Mue22

                SHM.setKey("TEST11");
                SHM.attach();
                SHM.create(sizeof(shm_BlockData));
                SHM.lock();

                shm_BlockData *from=new shm_BlockData;
                from->....;
                from->...;
                shm_BlockData to ;
                *
                to=(shm_BlockData
                )SHM.data();
                memcpy(to,from,sizeof(shm_BlockData));

                sorry, i find the code in my project....

                Maybe, the problem occured there ,

                struct control_data *data; -> struct control_data *data= new control_data;

                i miss allocate ...

                sorry, i don't know byte array method.. another programmer will help you!
                i use that(struct method) in my project. i hope to help you using my reply...:(

                R Offline
                R Offline
                r_spb
                wrote on last edited by
                #7

                @Mue22 Do you have a QString or a pointer in your struct?

                M 1 Reply Last reply
                0
                • R r_spb

                  @Mue22 Do you have a QString or a pointer in your struct?

                  M Offline
                  M Offline
                  Mue22
                  wrote on last edited by Mue22
                  #8

                  @r_spb
                  Yes! i test the QString in struct just before.

                  and in my oppinion, this method using memcpy, it make sharedmemory data to struct data, it doesnt matter to type..... maybe....

                  i can&#x27;t use english well...

                  1 Reply Last reply
                  0

                  • Login

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