Results 1 to 2 of 2

Thread: Create new QsharedMemory after crash

  1. #1
    Join Date
    Mar 2010
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Create new QsharedMemory after crash

    If application owning the QSharedMemory crashes, it will not detach so next time create will fail.
    From the document:
    QSharedMemory "owns" the shared memory segment. When the last thread or process that has an instance of QSharedMemory attached to a particular shared memory segment detaches from the segment by destroying its instance of QSharedMemory, the Unix kernel release the shared memory segment. But if that last thread or process crashes without running the QSharedMemory destructor, the shared memory segment survives the crash.


    My question is this: before I call create, if I try to atatch and detach will it detect that this is the last process and thus release the segment? If not, is there any Qt way to release the shared memory without using ipcrm?

    Here is a possible code snippet:

    Qt Code:
    1. QSharedMemory * hnd = NULL;
    2. QSharedMemory * pHnd = new QSharedMemory(strKey);
    3. if (pHnd)
    4. {
    5. // Try to attach and detach to handle crashed segments
    6. if (pHnd->attach(QSharedMemory::ReadOnly))
    7. pHnd->detach();
    8. // Now create brand new segment
    9. if (pHnd->create(size))
    10. hnd = pHnd;
    11. else
    12. delete pHnd;
    13. }
    14. return hnd; // Null if unsuccessful
    To copy to clipboard, switch view to plain text mode 

    Thanks
    Last edited by dashesy; 30th June 2012 at 00:25.

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

    Default Re: Create new QsharedMemory after crash

    Why don't you just try and see?
    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.


Similar Threads

  1. QSharedMemory example
    By weixj2003ld in forum Qt Programming
    Replies: 3
    Last Post: 9th October 2015, 12:14
  2. QSharedMemory
    By weixj2003ld in forum Qt Programming
    Replies: 1
    Last Post: 23rd August 2011, 11:49
  3. QSharedMemory Listening?
    By ManuMies in forum Qt Programming
    Replies: 2
    Last Post: 7th December 2010, 13:42
  4. about the QSharedMemory
    By banban0802 in forum Qt Programming
    Replies: 5
    Last Post: 10th August 2010, 12:08
  5. new vs QSharedMemory
    By JovianGhost in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2010, 00:34

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.