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. Creating Reference Object to Shared Memory
Forum Update on Monday, May 27th 2025

Creating Reference Object to Shared Memory

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 330 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.
  • S Offline
    S Offline
    savedsynner
    wrote on 27 Aug 2019, 00:30 last edited by savedsynner
    #1

    I went through the official QT version of using Shared memory, which involves putting the data into a buffer and then putting that buffer data into the shared memory.

    Instead, could we create an object that is a pointer to sharedMemory.constData() so that whenever we change a assign a member of the object, it writes it to shared memory(I'll be using psuedocode so feel free to correct my syntax but the question is what I'm trying to achieve possible in QT):

    In the .h file:
    Class IPC {

    Public:

    int x;
    int y;
    }

    In the mainwindows.cpp file:

    //Our SharedMemory Object's name is sharedMemory

    if (!sharedMemory.create(size)) {
    ui.label->setText(tr("Unable to create shared memory segment."));
    return;
    }
    //Instantiate the IPC class' ipcObject
    IPC *ipcObject = &sharedMemory.constData(since constData returns a pointer, not sure if we have to put bother the * and &)

    ipcObject.x = 4
    ipcObject.y = 5

    Basically, what I'm doing(trying to do) is create a pointer to the sharedMemory.constData(the data within the shared memory segment) and that pointer is an object(ipcObject). Then, rather than reference the sharedMemory object, I reference the ipcObject object and can access it's members(x and y) and can set variables and such and another QT program with the same class definition can access the same shared memory and access say ipcObject.x.

    The idea is instead of using memcpy and locking the shared memory everytime I want to copy something into the shared memory, I create an object that points to the address of the shared memory and can then set and access variables within the shared memory. I am aware that QT recommends you lock() the shared memory whenever you read from or write to it but is that required or just good safe form? I can see how it would be harmful to just leave a block of shared memory open(not locked) for a long period of time

    This is essentially (trying to) create a shared object(ipcObject) between two independent QT applications running on the same machine. Because the object points to the sharedMemory address, will this work?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Kent-Dorfman
      wrote on 28 Aug 2019, 02:13 last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • K Offline
        K Offline
        Kent-Dorfman
        wrote on 29 Aug 2019, 03:10 last edited by
        #3

        sure you can do that, but you better use locking on writes, and conditionally on reads (such as if the reads are not atomic or access multiple elements in one operation). locks are not just good form. they are necessary when you have more than one task accessing the same memory concurrently.

        Also, you should probably use data() method instead.

        So yeah, you can put a wrapper around QSharedMemory that does locking...I would.

        1 Reply Last reply
        0

        3/3

        29 Aug 2019, 03:10

        • Login

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