Results 1 to 8 of 8

Thread: QSharedMemory and C++ objects

  1. #1
    Join Date
    Aug 2009
    Posts
    140
    Thanks
    22
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QSharedMemory and C++ objects

    Hello,

    I can get QSharedMemory to work just fine with arrays of ints, but not with a vector, so I'm wondering if anyone knows how to do the latter. Obviously it's simple enough to convert a vector of ints into an array of ints and share that, but I want to be able to share complex C++ classes so I figured I would just try to get it to work with a vector first. Anyway, here's what works:

    Qt Code:
    1. void MemWriter::Write()
    2. {
    3. int* data = (int*)mem->data(); //'mem' is QSharedMemory, created elsewhere
    4. for (int i=min; i<max+1; ++i)
    5. data[i-min] = i;
    6. }
    7.  
    8. void MemReader::Read()
    9. {
    10. int* data = (int*)(mem->data()); //'mem' is QSharedMemory, created elsewhere
    11. for (int i=0; i<range; ++i)
    12. qDebug() << data[i];
    13. }
    To copy to clipboard, switch view to plain text mode 

    I.e., the the integers 0-9 are just written into the shared memory block and then read out again. MemWriter::Write() and MemReader::Read() are in different processes.

    Here's what doesn't work:

    Qt Code:
    1. void MemWriter::Write()
    2. {
    3. vector<int>* vec = reinterpret_cast< vector<int>* >(mem->data()); //'mem' is QSharedMemory, created elsewhere
    4. for (int i=min; i<max+1; ++i)
    5. vec->push_back(i);
    6. }
    7.  
    8. void MemReader::Read()
    9. {
    10. vector<int>* data = reinterpret_cast< vector<int>* >(mem->data()); //'mem' is QSharedMemory, created elsewhere
    11. for (vector<int>::iterator it=data->begin(); it!=data->end(); ++it)
    12. qDebug() << *it;
    13. }
    To copy to clipboard, switch view to plain text mode 

    This crashes when Read() is called. I've tried a few different variations on this, such as static_cast instead of reinterpret_cast--in that case it crashes/produces undefined behavior during Write(). Am I stuck with C int/double/etc arrays?

    Matt

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSharedMemory and C++ objects

    And how did you create this 'mem' with std::vector inside?
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSharedMemory and C++ objects

    QVector (and other C++ objects) contain pointers inside. These pointers are pointers inside the creating process's memory. If you put that in shared memory, the other process's accessing it will try that address - which is invalid (hopefully) for them.

    You need either special containers or at least allocators to put containers into shared memory. (See e.g. Boost Interprocess)
    Anything without pointers can be used (so no QString, std::string either, for example).

    HTH

  4. The following user says thank you to caduel for this useful post:

    MattPhillips (9th December 2009)

  5. #4
    Join Date
    Aug 2009
    Posts
    140
    Thanks
    22
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSharedMemory and C++ objects

    Caduel,

    Thanks, that was exactly it. Just to convince myself I created two structs,

    Qt Code:
    1. struct A
    2. {
    3. char ch;
    4. int i;
    5. double d;
    6. };
    7.  
    8. struct B
    9. {
    10. char ch;
    11. int* i;
    12. double d;
    13. };
    To copy to clipboard, switch view to plain text mode 

    and indeed, A worked fine but B did not. Thanks again. Thanks too faldzip for your consideration.

    Matt

  6. #5
    Join Date
    Sep 2009
    Location
    phoenix, AZ
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSharedMemory and C++ objects

    Hey Matts,

    did you find a way to share pointers to variables ( i.e. struct B ) ?

    thanks,

    Michael

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSharedMemory and C++ objects

    You can't do that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Feb 2010
    Location
    Poland
    Posts
    27
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSharedMemory and C++ objects

    don't exaggerate. probably there are some containers that allow you to share whole objects. just search.

  9. #8
    Join Date
    Aug 2009
    Posts
    140
    Thanks
    22
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSharedMemory and C++ objects

    Hi,

    It looks like the Boost IPC library has smart pointers which can be used within a shared memory block.

    http://www.boost.org/doc/libs/1_38_0....qg_offset_ptr

    But no dice with standard containers, in STL or I presume Qt as well.

    Matt

Similar Threads

  1. QSharedMemory won't attach
    By MattPhillips in forum Qt Programming
    Replies: 3
    Last Post: 27th November 2009, 15:45
  2. QSharedMemory in processes from different accounts
    By Ursa in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2009, 09:50

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.