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 multiple segment with QSharedMemory
Qt 6.11 is out! See what's new in the release blog

Share multiple segment with QSharedMemory

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.0k 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.
  • B Offline
    B Offline
    boumacmilan
    wrote on last edited by
    #1

    Hi every body

    i want to use QSharedMemory to share data between 2 applications using it for sharing one segment works perfectly but when i try 2 share more than one i have access just to the last one

    this my code

    sharedMemroy.cpp in the first application

    @
    void SharedMemory::loadIntoSharedMem(QString memoryKey,QString &data)
    {
    sharedMem.setKey(memoryKey);
    if (sharedMem.isAttached())
    {
    sharedMem.detach();
    }
    if( data.length())
    {
    // load into shared memory
    QBuffer buffer;
    buffer.open(QBuffer::ReadWrite);
    QDataStream out(&buffer);
    out << data;
    int size = buffer.size();

         if (!sharedMem.create(size)) {
             qDebug()<<"Unable to create shared memory segment."<<sharedMem.isAttached()<<" "<<sharedMem.error();
    
         }
    
         sharedMem.lock();
         char *to = (char*)sharedMem.data();
         const char *from = buffer.data().data();
         memcpy(to, from, qMin(sharedMem.size(), size));
         sharedMem.unlock();
        }
    else
        {
          qDebug()<< "no data to share"
        }
    
    }
    

    @

    and in main

    @
    SharedMemory sh;
    sh.loadIntoSharedMem("memo1",data1 );
    sh.loadIntoSharedMem("memo2",data2 );
    sh.loadIntoSharedMem("memo3",data3 );
    @

    and on showSharedData.cpp in the seconde application

    @
    QString ShowSharedMemory::loadFromSharedMem(QString memoryKey)
    {
    sharedMem.setKey(memoryKey);
    if (!sharedMem.attach())
    {
    qDebug()<<"Unable to load!";
    return"";
    }

    QBuffer buffer;
    QDataStream in(&buffer);
    QString text;
    
    sharedMem.lock();
    buffer.setData((char*)sharedMem.constData(), sharedMem.size());
    buffer.open(QBuffer::ReadOnly);
    in >> text;
    sharedMem.unlock();
    
    sharedMem.detach();
    return text;
    

    }
    @

    and in main

    @
    data1=showMemory.loadFromSharedMem("memo1");
    data2=showMemory.loadFromSharedMem("memo2");
    data3=showMemory.loadFromSharedMem("memo3");
    @

    thanks for your help

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
      wrote on last edited by
      #2

      in the first source code I see shared memory key as 'mem' etc. In the reader code you specifying the sharedmemory keys as dddd etc. Why are giving different keys for reader and writer ? It need to be same key for both reader and writer.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      https://www.pthinks.com

      1 Reply Last reply
      0
      • B Offline
        B Offline
        boumacmilan
        wrote on last edited by
        #3

        @dheerendra thanks aswring
        yes sur it was just a mistake of copy/past :p
        but on the code there are the same

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          If I remember the QSharedMemory mechanism correctly and following your code, you are creating and destroying the two first and only the third lives long enough to actually share some data

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
            wrote on last edited by
            #5

            If possible can you share you entire code. I can look and share you the right solution.

            Dheerendra
            @Community Service
            Certified Qt Specialist
            https://www.pthinks.com

            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