QSHaredMemory在Windows中的行为不一致

QSharedMemory inconsistent behaviour in Windows

本文关键字:不一致 Windows QSHaredMemory      更新时间:2023-10-16

我正在玩QSharedMemory,我不确定我只是发现一个严重的错误还是我做错了什么。情况是,文档说如果没有相同键的内存,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中找到了一个非常大的错误还是我做错了什么?

ps:此代码在Windows 7 x64

上运行

编辑:为了清楚起见,如果我两次运行该代码,第二个应用程序应检测到第一个应用程序,但不是。

编辑2:我在这里报告了一个错误

这绝对是一个错误,请阅读我的错误报告https://bugreports.qt.io/browse/qtbug-27765

我最近安装了一个补丁以解决此问题。如果您想解决这个问题,则需要投票。

我只是在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:似乎是一个错误,听起来很像:https://bugreports.qt.io/browse/qtbug-5123