Results 1 to 4 of 4

Thread: QSharedMemory example

  1. #1
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default QSharedMemory example

    I run the QSharedMemory example,that is, I start two instances of the executable,A and B.In A,I click "Load Image from File.." button,and in B I click "Display Image From shared Memory" button, and it runs well. Now,I close A,I click "Display Image From shared Memory" button again,but the infomation "Unable to attach to shared memory segment ,load an image first" shows.

    I think that the share memory should exist for instance B is existing.But....



    Qt Code:
    1. #include "dialog.h"
    2. #include <QFileDialog>
    3. #include <QBuffer>
    4. #include <QtCore/QDebug>
    5.  
    6. /*!
    7.   \class Dialog
    8.  
    9.   \brief This class is a simple example of how to use QSharedMemory.
    10.  
    11.   It is a simple dialog that presents a few buttons. To compile the
    12.   example, run make in qt/examples/ipc. Then run the executable twice
    13.   to create two processes running the dialog. In one of the processes,
    14.   press the button to load an image into a shared memory segment, and
    15.   then select an image file to load. Once the first process has loaded
    16.   and displayed the image, in the second process, press the button to
    17.   read the same image from shared memory. The second process displays
    18.   the same image loaded from its new loaction in shared memory.
    19.  */
    20.  
    21. /*!
    22.   The class contains a data member \l {QSharedMemory} {sharedMemory},
    23.   which is initialized with the key "QSharedMemoryExample" to force
    24.   all instances of Dialog to access the same shared memory segment.
    25.   The constructor also connects the clicked() signal from each of the
    26.   three dialog buttons to the slot function appropriate for handling
    27.   each button.
    28.  */
    29. Dialog::Dialog(QWidget *parent)
    30. : QDialog(parent), sharedMemory("QSharedMemoryExample")
    31. {
    32. ui.setupUi(this);
    33. connect(ui.loadFromFileButton, SIGNAL(clicked()), SLOT(loadFromFile()));
    34. connect(ui.loadFromSharedMemoryButton,
    35. SIGNAL(clicked()),
    36. SLOT(loadFromMemory()));
    37. setWindowTitle(tr("SharedMemory Example"));
    38. }
    39.  
    40. /*!
    41.   This slot function is called when the \tt {Load Image From File...}
    42.   button is pressed on the firs Dialog process. First, it tests
    43.   whether the process is already connected to a shared memory segment
    44.   and, if so, detaches from that segment. This ensures that we always
    45.   start the example from the beginning if we run it multiple times
    46.   with the same two Dialog processes. After detaching from an existing
    47.   shared memory segment, the user is prompted to select an image file.
    48.   The selected file is loaded into a QImage. The QImage is displayed
    49.   in the Dialog and streamed into a QBuffer with a QDataStream.
    50.  
    51.   Next, it gets a new shared memory segment from the system big enough
    52.   to hold the image data in the QBuffer, and it locks the segment to
    53.   prevent the second Dialog process from accessing it. Then it copies
    54.   the image from the QBuffer into the shared memory segment. Finally,
    55.   it unlocks the shared memory segment so the second Dialog process
    56.   can access it.
    57.  
    58.   After this function runs, the user is expected to press the \tt
    59.   {Load Image from Shared Memory} button on the second Dialog process.
    60.  
    61.   \sa loadFromMemory()
    62.   */
    63. void Dialog::loadFromFile()
    64. {
    65. if (sharedMemory.isAttached())
    66. detach();
    67.  
    68. ui.label->setText(tr("Select an image file"));
    69. QString fileName = QFileDialog::getOpenFileName(0, QString(), QString(),
    70. tr("Images (*.png *.xpm *.jpg)"));
    71. QImage image;
    72. if (!image.load(fileName)) {
    73. ui.label->setText(tr("Selected file is not an image, please select another."));
    74. return;
    75. }
    76. ui.label->setPixmap(QPixmap::fromImage(image));
    77.  
    78. // load into shared memory
    79. QBuffer buffer;
    80. buffer.open(QBuffer::ReadWrite);
    81. QDataStream out(&buffer);
    82. out << image;
    83. int size = buffer.size();
    84.  
    85. if (!sharedMemory.create(size)) {
    86. ui.label->setText(tr("Unable to create shared memory segment."));
    87. return;
    88. }
    89. sharedMemory.lock();
    90. char *to = (char*)sharedMemory.data();
    91. const char *from = buffer.data().data();
    92. memcpy(to, from, qMin(sharedMemory.size(), size));
    93. sharedMemory.unlock();
    94. }
    95.  
    96. /*!
    97.   This slot function is called in the second Dialog process, when the
    98.   user presses the \tt {Load Image from Shared Memory} button. First,
    99.   it attaches the process to the shared memory segment created by the
    100.   first Dialog process. Then it locks the segment for exclusive
    101.   access, copies the image data from the segment into a QBuffer, and
    102.   streams the QBuffer into a QImage. Then it unlocks the shared memory
    103.   segment, detaches from it, and finally displays the QImage in the
    104.   Dialog.
    105.  
    106.   \sa loadFromFile()
    107.   */
    108. void Dialog::loadFromMemory()
    109. {
    110. if (!sharedMemory.attach()) {
    111. ui.label->setText(tr("Unable to attach to shared memory segment.\n" \
    112. "Load an image first."));
    113. return;
    114. }
    115.  
    116. QBuffer buffer;
    117. QDataStream in(&buffer);
    118. QImage image;
    119.  
    120. sharedMemory.lock();
    121. buffer.setData((char*)sharedMemory.constData(), sharedMemory.size());
    122. buffer.open(QBuffer::ReadOnly);
    123. in >> image;
    124. sharedMemory.unlock();
    125.  
    126. sharedMemory.detach();
    127. ui.label->setPixmap(QPixmap::fromImage(image));
    128. }
    129.  
    130. /*!
    To copy to clipboard, switch view to plain text mode 
    This private function is called by the destructor to detach the
    process from its shared memory segment. When the last process
    detaches from a shared memory segment, the system releases the
    shared memory.
    */
    void Dialog::detach()
    {
    if (!sharedMemory.detach())
    ui.label->setText(tr("Unable to detach from shared memory."));
    }
    Last edited by weixj2003ld; 24th August 2011 at 13:01. Reason: sorry

  2. #2
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: QSharedMemory example

    Well, the data exists in shared memory as long as some process is attached to it. If you look into the code, your instance B is detaching from the memory as soon as it reads the image, sama happens to the instance A when it is destroyed, so there is no process being attached anymore and memory segment is being destroyed.
    Don't write a post just to thank someone, use "Thanks" button.

  3. #3
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSharedMemory example

    it is not ok when I delete "sharedMemory.detach();" in fuction loadFromMemory.

  4. #4
    Join Date
    Oct 2015
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QSharedMemory example

    Sorry, not at thaeme
    Last edited by Djoni_D; 9th October 2015 at 13:22. Reason: removed sensitive data

Similar Threads

  1. QSharedMemory
    By weixj2003ld in forum Qt Programming
    Replies: 1
    Last Post: 23rd August 2011, 12:49
  2. QSharedMemory Listening?
    By ManuMies in forum Qt Programming
    Replies: 2
    Last Post: 7th December 2010, 14:42
  3. QSharedMemory problem
    By produktdotestow in forum Qt Programming
    Replies: 2
    Last Post: 18th November 2010, 22:08
  4. about the QSharedMemory
    By banban0802 in forum Qt Programming
    Replies: 5
    Last Post: 10th August 2010, 13:08
  5. new vs QSharedMemory
    By JovianGhost in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2010, 01:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.