【发布时间】:2015-02-18 09:34:02
【问题描述】:
我想将我的应用程序作为单个实例执行,目前我正在使用QSharedMemory 并且工作正常。我在 Ubuntu 12.04 上使用 Qt5.2.1。
下面是我的测试代码:
QApplication a(argc, argv);
a.processEvents();
const char* MEM_KEY = "56";
QSharedMemory sharedMem(MEM_KEY);
if( !sharedMem.create( 512, QSharedMemory::ReadWrite) )
{
QMessageBox msgBox;
msgBox.setText( QObject::tr("Can't start more than one instance of the application.") );
msgBox.setIcon( QMessageBox::Critical );
msgBox.exec();
exit(0);
}
MainWindow w;
w.show();
int p=0;
//p=p/0; // create exception here
return a.exec();
但是if 使应用程序崩溃(如上面的代码所示)。如果我再次启动应用程序,它会显示Can't start more than one instance of the application,这意味着即使之前的实例已经崩溃,它仍然存在。在我的情况下不应该发生这种情况。
在这种情况下如何重新启动我的应用程序?
编辑:
实际上我的原始项目包含很多源文件(上面的一个只是为了测试而制作的)。我想在我的原始项目中通过编辑最少的源文件来实现它,如果可能的话,只能通过编辑 main.cpp
【问题讨论】:
-
如果你尝试/catch你崩溃了会发生什么,在catch中解锁共享内存然后退出应用程序?
-
好的,有可能,但在我的情况下,我的原始项目包含许多源文件(上面只是为了测试而制作的),我想通过编辑最少的源文件在我的原始项目中实现它,如果可能的话,只能通过编辑 main.cpp
-
QSharedMemory::error()在sharedMem.create失败后返回什么? 题外话 - 你可以使用静态函数QMessageBox::critical(...)而不是你使用的 4 行 -
感谢您的建议,QSharedMemory::error() 在崩溃和实例存在时返回 4(之前的应用程序正常运行)。
-
我通过使用解决方案 [这里][1] [1] 得到了它的工作:stackoverflow.com/questions/5006547/…