Results 1 to 3 of 3

Thread: shared memory wont attach

  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 shared memory wont attach

    here is my code:

    Qt Code:
    1. QSharedMemory numofallplayers;
    2.  
    3. void sesman::run()
    4. {
    5. numofallplayers.setKey("numofallplayers");
    6. numofallplayers.create(sizeof(qint64), QSharedMemory::ReadWrite);
    7.  
    8. qDebug() << "session manager thread starts from" << QThread::currentThread();
    9. qDebug() << "seting total players to zero";
    10. sesman::SetNumOfAllPlayers(0);
    11. qDebug() << "num of players total: " << sesman::GetNumOfAllPlayers(); //just to check
    12. .
    13. .
    14. .
    15. cleanup();
    16. }
    17.  
    18.  
    19. void sesman::SetNumOfAllPlayers(qint64 totalplayers)
    20. {
    21. if(numofallplayers.isAttached()){
    22. numofallplayers.detach();
    23. }
    24. if(!numofallplayers.attach(QSharedMemory::ReadWrite)){
    25. //some error
    26. qDebug() << "attach numofallplayers failed" << numofallplayers.errorString();
    27. }
    28. numofallplayers.lock();
    29. qint64* totalpl = (qint64*)numofallplayers.data();
    30. *totalpl = totalplayers;
    31. numofallplayers.unlock();
    32. if(!numofallplayers.detach())
    33. qDebug() << "unable to detach";
    34. }
    To copy to clipboard, switch view to plain text mode 

    and i get following error

    seting total players to zero
    attach numofallplayers failed "QSharedMemory::handle:: UNIX key file doesn't exist"
    The program has unexpectedly finished.

    Can someone help? what is wrong with my code?

    thanks a lot for any help...

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: shared memory wont attach

    Can you check if the following line succeeded?

    Qt Code:
    1. numofallplayers.create(sizeof(qint64), QSharedMemory::ReadWrite);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    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: shared memory wont attach

    problem wasn't in shared memory, but in a threading I'm using... so now I know I have to call sharedmemory.create in beginning of every thread, so i make sure it's created before any thread attach it... and in threading you never know which thread will be first

Similar Threads

  1. Does Qt support shared memory ?
    By Shawn in forum Qt Programming
    Replies: 11
    Last Post: 3rd November 2009, 17:07
  2. QT shared memory
    By sweetsubha2020 in forum Qt Programming
    Replies: 2
    Last Post: 18th January 2009, 05:30
  3. Replies: 4
    Last Post: 29th December 2008, 19:50
  4. MySQL and shared-memory connection
    By patrikd in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2008, 16:53
  5. How to clear shared memory?
    By THRESHE in forum Qt Programming
    Replies: 4
    Last Post: 22nd May 2008, 18:28

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.