QSharedMemory 不会在应用程序崩溃时被删除 [英] QSharedMemory is not getting deleted on Application crash

查看:140
本文介绍了QSharedMemory 不会在应用程序崩溃时被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Qt C++ 实现一个应用程序,其中我使用了 QSharedMemory 来限制应用程序的多个实例.main.cpp中的相关代码段如下,

I am implementing an application using Qt C++ where I have used QSharedMemory to restrict multiple instances of the application. Relevant code segment in main.cpp is as follows,

QSharedMemory sharedMemory;
sharedMemory.setKey(SM_INSTANCE_KEY);

if (!sharedMemory.create(1))
{
    QMessageBox::warning(0, "Console", "An instance of this application is already running!" );
    exit(0); /* Exit, already a process is running */
}

在打开应用程序时,我可以看到已为我的应用程序创建了一个共享内存.(shmid 7045192, 大小 1B)

On opening the Application, I can see that a shared memory has been created for my application. (shmid 7045192, size 1B)

到目前为止一切顺利.当我的应用程序由于某种原因崩溃时会出现问题.崩溃时,sharedMemory 没有被清除,所以我无法再打开应用程序.当它崩溃时,附加的应用程序计数变为 0,但共享内存不会被删除.相关截图如下

So far so good. Problem arises when my application crashes for some reason. On crashing, the sharedMemory is not getting cleared, so I can't open the application anymore. When it crashes, attached application count becomes 0, but the shared memory does not get deleted. Relevant screen-shot is as follows

据我所知,由于共享内存的状态没有像其他共享内存一样标记为dest,所以即使没有任何附加进程也不会被删除.

As per as my understanding, as the status of the shared memory is not marked as dest like other shared memories, it is not getting deleted even when there is not any attached process.

所以,我的问题是有没有办法将共享内存的状态标记为 dest ?

So, my question is that is there any way of marking status of the Shared Memory as dest ?

推荐答案

引用 QSharedMemory 文档:

使用此类时,请注意以下平台差异:

When using this class, be aware of the following platform differences:

Windows:QSharedMemory 不拥有"共享内存段.当所有具有 QSharedMemory 实例的线程或进程附加到特定共享内存段的要么已销毁他们的 QSharedMemory 实例或退出,Windows 内核发布自动共享内存段.

Windows: QSharedMemory does not "own" the shared memory segment. When all threads or processes that have an instance of QSharedMemory attached to a particular shared memory segment have either destroyed their instance of QSharedMemory or exited, the Windows kernel releases the shared memory segment automatically.

Unix:QSharedMemory拥有"共享内存段.当最后一个线程或进程附加了 QSharedMemory 的实例时到特定的共享内存段通过以下方式从该段分离销毁它的 QSharedMemory 实例,Unix 内核释放共享内存段.但是如果最后一个线程或进程崩溃不运行 QSharedMemory 析构函数,共享内存段在崩溃中幸存下来.

Unix: 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.

HP-UX:每个进程只允许一个附加到共享内存段.这意味着 QSharedMemory 不应跨HP-UX 中同一进程中的多个线程.

HP-UX: Only one attach to a shared memory segment is allowed per process. This means that QSharedMemory should not be used across multiple threads in the same process in HP-UX.

几年前我在 Linux 上添加了同样的问题,我们通过执行以下步骤解决了这个问题:

I've add the same issue on Linux few years ago, we solved the problem by doing those steps:

// Pseudo code
if (create_share_memory() == failed)
{
    // The failure may be caused by the shm already existing
    attach()
    detach() // This should delete the shm if no process use it
    if (create_share_memory() == failed)
    {
       // We really cannot create the share memory, report the error
       return failed
    }
}
return ok

这篇关于QSharedMemory 不会在应用程序崩溃时被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆