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. Unable to read data from share memory
Qt 6.11 is out! See what's new in the release blog

Unable to read data from share memory

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.3k Views
  • 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
    Biswa
    wrote on last edited by
    #1

    Hii all,
    I am try to read data from share memory and it is showing
    "Unable to attach to shared memory segment: "QSharedMemory::handle:: UNIX key file doesn't exist" "
    hear is my source code for read data from share memory```
    code_text

    #include <QSharedMemory>
    #include <QDebug>
    
    int main(int argc, char *argv[]) {
        QCoreApplication a(argc, argv);
    
        // Name of the shared memory segment
        QString shmKey = "my_memory";
    
        // Create a shared memory object
        QSharedMemory sharedMemory(shmKey);
    
        // Attach to the shared memory segment
        if (!sharedMemory.attach()) {
            qDebug() << "Unable to attach to shared memory segment:" << sharedMemory.errorString();
            return 1;
        }
    
        // Read data from the shared memory
        char *from = static_cast<char*>(sharedMemory.data());
        QString message = QString::fromUtf8(from);
    
        qDebug() << "Data read from shared memory:" << message;
    
        return a.exec();
    }
    JonBJ 1 Reply Last reply
    0
    • B Biswa

      Hii all,
      I am try to read data from share memory and it is showing
      "Unable to attach to shared memory segment: "QSharedMemory::handle:: UNIX key file doesn't exist" "
      hear is my source code for read data from share memory```
      code_text

      #include <QSharedMemory>
      #include <QDebug>
      
      int main(int argc, char *argv[]) {
          QCoreApplication a(argc, argv);
      
          // Name of the shared memory segment
          QString shmKey = "my_memory";
      
          // Create a shared memory object
          QSharedMemory sharedMemory(shmKey);
      
          // Attach to the shared memory segment
          if (!sharedMemory.attach()) {
              qDebug() << "Unable to attach to shared memory segment:" << sharedMemory.errorString();
              return 1;
          }
      
          // Read data from the shared memory
          char *from = static_cast<char*>(sharedMemory.data());
          QString message = QString::fromUtf8(from);
      
          qDebug() << "Data read from shared memory:" << message;
      
          return a.exec();
      }
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @Biswa
      As https://doc.qt.io/qt-6/qsharedmemory.html#details says:

      One QSharedMemory object must create() the segment and this call specifies the size of the segment. All other processes simply attach() to the segment that must already exist.

      So where have you, either in this process or another process, called bool QSharedMemory::create(qsizetype size, QSharedMemory::AccessMode mode = ReadWrite)? I imagine that is what error message UNIX key file doesn't exist indicates. Tell me, does the message really include the word UNIX in it??

      1 Reply Last reply
      2
      • B Offline
        B Offline
        Biswa
        wrote on last edited by
        #3

        @JonB
        I want to write data in Shared Memory using python language and that data i want to read from Shared Memory.

        B 1 Reply Last reply
        0
        • B Biswa

          @JonB
          I want to write data in Shared Memory using python language and that data i want to read from Shared Memory.

          B Offline
          B Offline
          Bonnie
          wrote on last edited by
          #4

          @Biswa Then how do you create shared memory in python? Can you share the code? Are you sure they are using the same mechanism?
          One thing need to be noticed is that, when you create a QSharedMemory object by "QSharedMemory sharedMemory(shmKey);", shmKey will not be the actual key used by the native API.

          B 1 Reply Last reply
          1
          • B Bonnie

            @Biswa Then how do you create shared memory in python? Can you share the code? Are you sure they are using the same mechanism?
            One thing need to be noticed is that, when you create a QSharedMemory object by "QSharedMemory sharedMemory(shmKey);", shmKey will not be the actual key used by the native API.

            B Offline
            B Offline
            Biswa
            wrote on last edited by Biswa
            #5

            @Bonnie @Bonnie
            for writing data in shared memory am using pythoncode

            import posix_ipc
            import mmap
            import os
            import struct
            
            SHARED_MEMORY_NAME = "/my_memory123"
            MEMORY_SIZE = 1024
            
            # Create or open shared memory segment
            shared_memory = posix_ipc.SharedMemory(SHARED_MEMORY_NAME, posix_ipc.O_CREAT, size=MEMORY_SIZE)
            
            # Map the shared memory segment to memory
            mapfile = mmap.mmap(shared_memory.fd, MEMORY_SIZE)
            
            # Example integers to write
            integers = [0x02, 0x03, 0x04, 0x05]  # List of integers
            # Convert integers to bytes
            data = struct.pack(f'{len(integers)}I', *integers)
            
            # Write integer data to shared memory
            mapfile.seek(0)  # Move to the start of the shared memory segment
            mapfile.write(data)
            
            print("Integer data written to shared memory.")
            
            # Clean up
            mapfile.close()
            os.close(shared_memory.fd)
            
            B 1 Reply Last reply
            0
            • B Biswa

              @Bonnie @Bonnie
              for writing data in shared memory am using pythoncode

              import posix_ipc
              import mmap
              import os
              import struct
              
              SHARED_MEMORY_NAME = "/my_memory123"
              MEMORY_SIZE = 1024
              
              # Create or open shared memory segment
              shared_memory = posix_ipc.SharedMemory(SHARED_MEMORY_NAME, posix_ipc.O_CREAT, size=MEMORY_SIZE)
              
              # Map the shared memory segment to memory
              mapfile = mmap.mmap(shared_memory.fd, MEMORY_SIZE)
              
              # Example integers to write
              integers = [0x02, 0x03, 0x04, 0x05]  # List of integers
              # Convert integers to bytes
              data = struct.pack(f'{len(integers)}I', *integers)
              
              # Write integer data to shared memory
              mapfile.seek(0)  # Move to the start of the shared memory segment
              mapfile.write(data)
              
              print("Integer data written to shared memory.")
              
              # Clean up
              mapfile.close()
              os.close(shared_memory.fd)
              
              B Offline
              B Offline
              Bonnie
              wrote on last edited by Bonnie
              #6

              @Biswa Seems this posix_ipc and QSharedMemory both use shm_open API under POSIX, I suppose this could work.
              As I said, here we need to let QSharedMemory use "/my_memory123" as the native API key (to call shm_open).
              So instead of creating a QSharedMemory with key directly, try to use

              QSharedMemory sharedMemory;
              sharedMemory.setNativeKey("/my_memory123");
              

              P.S. There's some helpful information in Qt's documentation about this kind of case.
              For Qt5, read the Warning part of https://doc.qt.io/qt-5/qsharedmemory.html#details
              For Qt6, read https://doc.qt.io/qt-6/native-ipc-keys.html#interoperability-with-non-qt-applications

              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