【发布时间】:2012-10-29 11:03:14
【问题描述】:
我在玩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-27744
【问题讨论】:
-
这只是一个猜测,文档没有说,但似乎从同一个线程多次调用
create会返回 true...你为什么要从同一个线程多次调用它还是? -
@hyde 这只是一个测试。无论如何,如果我两次运行相同的应用程序,我会得到相同的结果。
标签: c++ qt qt4 shared-memory