QSharedMemory: freeze on attach to locked memory.

  • Greetings.

    This code causes a program to hang:
    @QSharedMemory test{"test"};
    test.create(1024); // create and attach
    test.lock(); // lock

    QSharedMemory test2{"test"};
    test2.attach(); // freezes@

    The same happens on detach. Why can this happen - one process can locks a memory, starts to write and throws. Yeah, this is a bad design but it can be. Another process, that knows nothing about the first one trying to attach to memory... and freezes.

    I can't find any way to know that memory is locked. And does that means that attach() works like semaphore, i.e. waits for memory release? Also the same about detach() - should I lock before detaching?

    P.S.: I can't post from chrome or firefox. And there were problems with registering from this browsers. Only IE works for me.

  • Moderators

    A bad design is not called bad design just for the sake of the principle but because it causes problems, bugs and errors. Like here.

    lock() and unlock() should be used on a tight bound around writing/reading. Not "globally". And if you throw from within the code surrounded by a lock you should make sure you unlocked it as a cleanup step.

    attach() waits until the shared memory region is unlocked. If you don't unlock it will freeze. You need to fix the locking app.

    You should definitely not lock before detaching. lock/unlock only before/after reading and writing.

    Both Chrome and FF work fine for me. Maybe too eager ad blocking plugin or something?

  • Yes, you right. I'll review our code. Thank you.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.