我在玩QSharedMemory,我不确定我是发现了一个严重的bug,还是做错了什么。情况是,文档说如果不存在具有相同键的内存,则QSharedMemory::create()应该返回true,否则它应该返回false,并且应该检查QSharedMemory::error()以查看发生了什么。
我当前的代码是:
QSharedMemory sm("smtest");
sm.setKey("smtest"); // <--- not needed as I already set the key in the initializator, but I'm leaving it anyways, just for the test
qDebug() << sm.create(1);
qDebug() << sm.create(1); //<--- I expect this to return false, but it returns true.
qDebug() << sm.error(); //<--. I expect this to return QSharedMemory::AlreadyExists, but QSharedMemory::NoError is returned instead.
//wtf?!我的问题是:我是刚刚在Qt4中发现了一个非常大的bug,还是我做错了什么?
PS:此代码在Windows7 x64上运行
编辑:为了清楚起见,如果我运行该代码两次,第二个应用程序应该会检测到第一个应用程序,但它没有。
编辑2:我在https://bugreports.qt.io/browse/QTBUG-27744这里报告了一个错误
发布于 2012-10-29 19:15:18
我只是在Linux上运行以下代码:
#include <QCoreApplication>
#include <QSharedMemory>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSharedMemory sm("smtest");
sm.setKey("smtest"); // <--- not needed as I already set the key in the initializator, but I'm leaving it anyways, just for the test
qDebug() << sm.create(1);
qDebug() << sm.create(1); //<--- I expect this to return false, but it returns true.
qDebug() << sm.error(); //<--. I expect this to return QSharedMemory::AlreadyExists, but QSharedMemory::NoError is returned instead.
//wtf?!
return 0;
}在第一次运行中得到了
true
false
4 会不会是你没有创建QCoreApplication?很多Qt的东西往往依赖于它的创建。
编辑:要强调的是,上述情况只在第一次运行时发生。随后的运行给出了false-false总是。
Edit2:在Windows上,结果是正确的-对我来说也是如此。
Edit3:看起来是个bug,听起来非常像这样:https://bugreports.qt.io/browse/QTBUG-5123
发布于 2013-03-31 19:31:57
这绝对是一个错误,请阅读我的错误报告https://bugreports.qt.io/browse/QTBUG-27765
我最近附加了一个补丁来修复这个问题。如果你想解决这个问题,你需要投票。
https://stackoverflow.com/questions/13120374
复制相似问题