Results 1 to 12 of 12

Thread: How do I read shared memory data written by Qt using boost ?

  1. #1
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default How do I read shared memory data written by Qt using boost ?

    I'm writing a string into QSharedMemory and after writing, I need to read it from boost program. I tried this, but didn't work.
    I read the data in shared memory from the same Qt program, and I see the string.

    Below is Qt program, from which I'm writing into shared memory with key ShrdMem
    Qt Code:
    1. // Slot, which is invoked on click of button.
    2. void CDialog::loadString()
    3. {
    4. if(sharedMemory.isAttached())
    5. {
    6. if(!sharedMemory.detach())
    7. {
    8. lbl->setText("Unable to detach from Shared Memory");
    9. return;
    10. }
    11. }
    12.  
    13. char sString[] = "my string";
    14.  
    15. QBuffer buffer;
    16. buffer.open(QBuffer::ReadWrite);
    17.  
    18. QDataStream out(&buffer);
    19. out << sString;
    20.  
    21. int size = buffer.size();
    22. qDebug() << size;
    23.  
    24. if(!sharedMemory.create(size))
    25. {
    26. lbl->setText("Unable to create shared memory segment");
    27. qDebug() << lbl->text();
    28. }
    29. sharedMemory.lock();
    30. char *to = (char *) sharedMemory.data();
    31. const char *from = buffer.data();
    32. memcpy(to, from, qMin(sharedMemory.size(), size));
    33. sharedMemory.unlock();
    34.  
    35. char * str;
    36. QDataStream in(&buffer);
    37. sharedMemory.lock();
    38. buffer.setData((char *)sharedMemory.constData(), sharedMemory.size());
    39. buffer.open(QBuffer::ReadOnly);
    40. in >> str;
    41. sharedMemory.unlock();
    42. qDebug() << str;
    43. }
    To copy to clipboard, switch view to plain text mode 
    I'm trying to read the ShrdMem using Boost as shown below.
    Qt Code:
    1. //NQShrdMem.cpp
    2. int main()
    3. {
    4. boost::interprocess::shared_memory_object shdmem(boost::interprocess::open_only, "ShrdMem", boost::interprocess::read_only);
    5.  
    6. boost::interprocess::offset_t size;
    7. if (shdmem.get_size(size))
    8. std::cout << "Shared Mem Size: " << size << std::endl;
    9.  
    10. boost::interprocess::mapped_region region2(shdmem, boost::interprocess::read_only);
    11. char *i2 = static_cast<char *>(region2.get_address());
    12. std::cout << i2 << std::endl;
    13. return 0;
    14. }
    To copy to clipboard, switch view to plain text mode 

    I'm compiling the Boost program in cygwin, using g++:
    Qt Code:
    1. g++ -I /cygdrive/c/boost/boost_1_53_0/ NQShrdMem.cpp -o NQShrdMem /cygdrive/c/boost_lib/boost/bin.v2/libs/date_time/build/gcc-mingw-4.7.2/release/link-static/threading-multi/libboost_date_time-mgw47-mt-1_53.a
    To copy to clipboard, switch view to plain text mode 

    Kindly help me. Thank you.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How do I read shared memory data written by Qt using boost ?

    You are using QDataStream in a setting where you are expecting a non-Qt program to read the data. There will be more data in the output stream than you are likely expecting to see: you need to account for that at the receiver. Read the docs for QDataStream:perator<<(const char *s) carefully or do not use QDataStream.

    BTW: It doesn't work is not a useful description. It helps if you let us know what's not working? What did you expect to happen? What is happening? etc.

  3. #3
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How do I read shared memory data written by Qt using boost ?

    You are using QDataStream in a setting where you are expecting a non-Qt program to read the data.
    Kindly tell me what I have to use ? I read the docs but couldn't get much.

    what's not working? What did you expect to happen? What is happening?
    1. First, I'm executing the Qt program to write to shared memory.
    2. Then executing the Non-Qt(Boost program).
    3. Expecting the Boost program to display size (std::cout << "Shared Mem Size: " << size << std::endl; ) and string (std::cout << i2 << std::endl;). But no result, in cygwin the promt appears again.

    Thank you.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How do I read shared memory data written by Qt using boost ?

    If you want total control of the bytes placed in the shared memory segment then you can either put them into a QByteArray directly or you can use QIODevice::write() against a QBuffer.

    I suggest you dump buffer.data().toHex() and inspect what QDataStream actually put into the buffer. Your generic C++ is expecting the buffer to start with a '\0' terminated string but it does not; it starts with a 4-byte length integer as described in the QDataStream docs. For a short string 3 of those 4 bytes will be '\0', terminating any string you read. The actual string component may also not have the terminating '\0' any longer.

  5. #5
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How do I read shared memory data written by Qt using boost ?

    I tried changing QByteArray directly instead of QBuffer and dumped data to shared memory.
    I tried to read the same using qDebug(), which gave me the same string that I'm passing (my string) but with lot of zeroes appended.
    Qt Code:
    1. // Hex Value
    2. 6d7920737472696e670000000000000000000000000000000000(so many zeroes, so I deleted here.)
    To copy to clipboard, switch view to plain text mode 

    But still I'm not able to get the same in Non-Qt(Boost) program's output.

    I did the following changes in Qt program
    Qt Code:
    1. void CDialog::loadFromFile()
    2. {
    3. if(sharedMemory.isAttached())
    4. {
    5. if(!sharedMemory.detach())
    6. {
    7. lbl->setText("Unable to detach from Shared Memory");
    8. return;
    9. }
    10. }
    11.  
    12. char sString[] = "my string";
    13.  
    14. QByteArray out(sString, 10);
    15.  
    16. int size = out.size();
    17. qDebug() << size;
    18.  
    19. if(!sharedMemory.create(size))
    20. {
    21. lbl->setText("Unable to create shared memory segment");
    22. qDebug() << lbl->text();
    23. }
    24. sharedMemory.lock();
    25. char *to = (char *) sharedMemory.data();
    26. const char *from = out.data();
    27. memcpy(to, from, qMin(sharedMemory.size(), size));
    28. sharedMemory.unlock();
    29.  
    30. sharedMemory.lock();
    31. in.setRawData((char *)sharedMemory.constData(), sharedMemory.size());
    32. sharedMemory.unlock();
    33. qDebug() << in.toHex();
    34. }
    To copy to clipboard, switch view to plain text mode 

    Thank you for helping.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How do I read shared memory data written by Qt using boost ?

    Quote Originally Posted by rawfool View Post
    3. Expecting the Boost program to display size (std::cout << "Shared Mem Size: " << size << std::endl; ) and string (std::cout << i2 << std::endl;). But no result, in cygwin the promt appears again.
    If you don't get the size output, doesn't that mean that shared_memory_object::get_size() has returned false?
    Wouldn't that indicate that you didn't access the shared memory correctly?

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    rawfool (17th May 2013)

  8. #7
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How do I read shared memory data written by Qt using boost ?

    Yeah, but I called the shared memory using the same key specified in the Qt program. I think that is the only way to access a shared memory. Is there anything that needs to be done ?
    Thank you.

  9. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How do I read shared memory data written by Qt using boost ?

    On my Linux system QSharedMemory creates a file with the path given by key(). The generated key will be in /tmp. So for setKey("FOO") I get file "/tmp/qipc_sharedmemory_FOOfeab40e1fca77c7360ccca1481bb8 ba5f919ce3a" (a predictable name). If you use setNativeKey() you can explicitly direct the file name and location.

    On the same system Boost will look for the specified shared memory block name under /dev/shm. So,
    Qt Code:
    1. shared_memory_object shdmem(open_only, "FOO", boost::interprocess::read_only);
    To copy to clipboard, switch view to plain text mode 
    looks for, and fails to find, "/dev/shm/FOO"

    I expect that is what is going on with you. However, we are not privy to the behaviours and limits of Boost in your Cygwin environment.

  10. #9
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How do I read shared memory data written by Qt using boost ?

    Now in addition to that, I created a shared memory using Boost shared memory in Qt, wrote a string into that. Then tried to access the same using another Boost program compiled in Cygwin-g++. Here get_size() is giving 0 and string is not being read. But surprising thing is, if I delete the same shared memory (for the specified key) using remove statement in cygwin-g++ program, it's getting deleted.

    Qt Code:
    1. bool isRemoved = boost::interprocess::shared_memory_object::remove("ShrdMem");
    2. std::cout << "Value = " << isRemoved << std::endl; // Value = 1
    To copy to clipboard, switch view to plain text mode 

    Writing in Qt(using QSharedMemory), then reading using Boost shared memory - Not working as expected
    Writing in Qt(using boost shared memory), then reading using Boost shared memory - Not working as expected
    Writing in Boost, reading in Boost(both compiled & run in cygwin-g++) - Working fine (tested to read the string/int value).

    Any help pls. Thank you.
    Last edited by rawfool; 16th May 2013 at 09:08.

  11. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How do I read shared memory data written by Qt using boost ?

    Writing in Qt(using boost shared memory), then reading using Boost shared memory - Not working as expected
    Writing in Boost, reading in Boost(both compiled & run in cygwin-g++) - Working fine (tested to read the string/int value).
    These are identical options. You don't write "in Qt(using boost shared memory)"... Qt is not a programming language. You write in C++, just like the other option.

    I am assuming that your Qt application is not built in the Cygwin environment, i.e. it is native Windows, and that the shared memory environment that Cygwin gives its applications does not use Windows shared memory mechanisms. Consequently they do not meet in the middle.

    Why are you using Cygwin anyway?

  12. The following user says thank you to ChrisW67 for this useful post:

    rawfool (17th May 2013)

  13. #11
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How do I read shared memory data written by Qt using boost ?

    I'm developing a GUI application, which needs communication with other process(built using Visual C++). I'm developing my app in Qt and trying to emulate the other process shared memory using boost, so just thought to do it Cygwin, thinking shared memory can be accessed commonly which requires a key and the key is unique.

    The objective is to set-up a shared memory communication between two processes (one developed using Qt & other using Visual C++.
    I'm trying to get one of the below mentioned two options to workout -
    1. Using QSharedMemory from my side(GUI) and interacting with other process which is using Boost.
    2. Using Boost interprocess in Qt since the other process too is using Boost.

    Thank you.
    Last edited by rawfool; 16th May 2013 at 12:00.

  14. #12
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How do I read shared memory data written by Qt using boost ?

    I solved the issue.

    As ChrisW67 mentioned, the problem is in meeting point. May be the Cygwin-g++ program was looking in other folder (may be /tmp/ as mentioned above by @ChrisW67).
    I created a Non-Qt C++ program, using Qt creator and read the shared memory using Boost ipc. It worked fine, I was able to read the string that I wrote in Qt program(Boost Shared memory).

    Thank you ChrisW67 & anda_skoa
    Last edited by rawfool; 17th May 2013 at 12:25.

  15. The following user says thank you to rawfool for this useful post:


Similar Threads

  1. How to read value which written inside the QLineEdit
    By RENOLD in forum Qt Programming
    Replies: 4
    Last Post: 16th February 2012, 14:59
  2. shared memory - the right way to do it
    By fearu in forum Newbie
    Replies: 0
    Last Post: 27th October 2011, 00:46
  3. Replies: 1
    Last Post: 18th October 2010, 17:07
  4. QPixmap: X11 "memory leak" due to shared pixmap data?
    By chezifresh in forum Qt Programming
    Replies: 0
    Last Post: 21st April 2009, 20:53
  5. QT shared memory
    By sweetsubha2020 in forum Qt Programming
    Replies: 2
    Last Post: 18th January 2009, 06:30

Tags for this Thread

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.