Results 1 to 2 of 2

Thread: problem with qsharedmemory

  1. #1
    Join Date
    May 2010
    Location
    slovakia
    Posts
    47
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Maemo/MeeGo

    Default problem with qsharedmemory

    i got following code and it looks like due to this code application crash (but still not sure, coz app is multithreaded)...

    code Code:
    1. void simplex_user::user_to_mem()
    2. {
    3. if(!usershm.isAttached())
    4. usershm.attach(QSharedMemory::ReadWrite);
    5. QByteArray usercache;
    6. QDataStream ucd(&usercache, QIODevice::ReadWrite);
    7. usercache.clear();
    8. usercache.resize(44+1);
    9. ucd.device()->seek(0);
    10.  
    11. ucd << (quint8)this->status << (quint8)this->speed << (quint8)this->playernum // 0 1 2
    12. << (quint8)this->lookat << (quint8)this->key1 << (quint8)this->key2 // 3 4 5
    13. << (quint8)this->gotpowerup1 << (quint8)this->gotpowerup2 // 6 7
    14. << (qint8)this->yoffset << (qint8)this->xoffset << (quint16)this->pos // 8 9 10-11
    15. << (quint64)this->score << (quint64)this->adminkey << (quint64)this->playerid // 12-19 20-27 28-35
    16. << (quint64)this->achievements << (quint8)this->flags; // 36-43 44
    17. usershm.lock();
    18. char *to = (char*)usershm.data();
    19. const char *from = this->data.data();
    20. memcpy(to, from, qMin(usershm.size(), usershm.size()));
    21. usershm.unlock();
    22.  
    23.  
    24. }
    25.  
    26. void simplex_user::mem_to_user()
    27. {
    28. bool ok;
    29. QByteArray usercache;
    30. usercache.resize(44+1);
    31.  
    32. if(!usershm.isAttached())
    33. usershm.attach(QSharedMemory::ReadWrite);
    34. usershm.lock();
    35. usercache = QByteArray((char*)usershm.constData(), usershm.size()+1);
    36. this->data=usercache;
    37. usershm.unlock();
    38. this->status=usercache.mid(0,1).toHex().toUInt(&ok, 16);
    39. this->speed=usercache.mid(1,1).toHex().toUInt(&ok, 16);
    40. this->playernum=usercache.mid(2,1).toHex().toUInt(&ok, 16);
    41. this->lookat=usercache.mid(3,1).toHex().toUInt(&ok, 16);
    42. this->key1=usercache.mid(4,1).toHex().toUInt(&ok, 16);
    43. this->key2=usercache.mid(5,1).toHex().toUInt(&ok, 16);
    44. this->gotpowerup1=usercache.mid(6,1).toHex().toUInt(&ok, 16);
    45. this->gotpowerup2=usercache.mid(7,1).toHex().toUInt(&ok, 16);
    46. this->yoffset=usercache.mid(8,1).toHex().toUInt(&ok, 16);
    47. this->xoffset=usercache.mid(9,1).toHex().toUInt(&ok, 16);
    48. this->pos=usercache.mid(10,2).toHex().toUShort(&ok, 16);
    49. this->score=usercache.mid(12,8).toHex().toULongLong(&ok, 16);
    50. this->adminkey=usercache.mid(20,8).toHex().toULongLong(&ok, 16);
    51. this->playerid=usercache.mid(28,8).toHex().toULongLong(&ok, 16);
    52. this->achievements=usercache.mid(36,8).toHex().toULongLong(&ok, 16);
    53. this->flags=usercache.mid(44,8).toHex().toULongLong(&ok, 16);
    54. }
    To copy to clipboard, switch view to plain text mode 

    is anything wrong with this code?

  2. #2
    Join Date
    May 2010
    Location
    slovakia
    Posts
    47
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Maemo/MeeGo

    Default Re: problem with qsharedmemory

    code Code:
    1. usershm.lock();
    2. char *to = (char*)usershm.data();
    3. const char *from = this->data.data();
    4. memcpy(to, from, qMin(usershm.size(), usershm.size()));
    5. usershm.unlock();
    To copy to clipboard, switch view to plain text mode 

    fixed... usercache replaced this->data.data();... but app still crash...

Similar Threads

  1. QSharedMemory and C++ objects
    By MattPhillips in forum Qt Programming
    Replies: 7
    Last Post: 29th November 2010, 07:42
  2. about the QSharedMemory
    By banban0802 in forum Qt Programming
    Replies: 5
    Last Post: 10th August 2010, 12:08
  3. new vs QSharedMemory
    By JovianGhost in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2010, 00:34
  4. QSharedMemory won't attach
    By MattPhillips in forum Qt Programming
    Replies: 3
    Last Post: 27th November 2009, 15:45
  5. 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.