Sur*_*ati 2 c++ qt shared-memory qsharedmemory
我正在使用Qt C++我用于QSharedMemory限制应用程序的多个实例的位置来实现应用程序.相关代码段main.cpp如下,
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 */
}
Run Code Online (Sandbox Code Playgroud)
在打开应用程序时,我可以看到已为我的应用程序创建了共享内存.(shmid7045192,1B size)
到现在为止还挺好.当我的应用程序由于某种原因崩溃时出现问题.在崩溃时,sharedMemory没有被清除,因此我无法再打开该应用程序.当它崩溃时,附加的应用程序计数变为0,但共享内存不会被删除.相关的屏幕截图如下
根据我的理解,由于共享内存的状态没有dest像其他共享内存那样标记,即使没有任何附加进程也不会被删除.
所以,我的问题是,有没有办法将共享内存的状态标记为dest?
引用QSharedMemory文件:
使用此类时,请注意以下平台差异:
Windows:QSharedMemory不"拥有"共享内存段.当具有附加到特定共享内存段的QSharedMemory实例的所有线程或进程已销毁其QSharedMemory实例或退出时,Windows内核会自动释放共享内存段.
Unix:QSharedMemory"拥有"共享内存段.当具有附加到特定共享内存段的QSharedMemory实例的最后一个线程或进程通过销毁其QSharedMemory实例而从该段中分离时,Unix内核将释放共享内存段.但是如果最后一个线程或进程在没有运行QSharedMemory析构函数的情况下崩溃,那么共享内存段将在崩溃中幸存下来.
HP-UX:每个进程只允许一个连接到共享内存段.这意味着不应在HP-UX中的同一进程中跨多个线程使用QSharedMemory.
我几年前在Linux上添加了同样的问题,我们通过这些步骤解决了这个问题:
// 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1100 次 |
| 最近记录: |