Reputation: 15089
I'm playing with QSharedMemory
and I'm not sure if I just found a serious bug or I'm doing something wrong. The case is that docs say that QSharedMemory::create()
should return true if no memory with the same key exists, otherwise it should return false and QSharedMemory::error()
should be checked to see what happened.
My current code is:
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?!
My question is: did I just found a really big bug in Qt4 or I'm doing something wrong?
PS: This code is ran on Windows 7 x64
EDIT: Just to make it clear, if I run that code twice, the second app should detect the first one, but it doesn't.
EDIT 2: I reported a bug here https://bugreports.qt.io/browse/QTBUG-27744
Upvotes: 3
Views: 1066
Reputation: 21
This is definitely a bug, please read my bug report https://bugreports.qt.io/browse/QTBUG-27765
I lately attached a patch to fix this. If you want this to be fixed you need to vote.
Upvotes: 2
Reputation: 62797
I just run this on 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;
}
and on first run got
true
false
4
Could it be that you don't have QCoreApplication created? A lot of Qt stuff tends to depend on that being created.
Edit: to emphasize, above only happens with first run. Subsequent runs give false-false always.
Edit2: On Windows, result is true-true for me too.
Edit3: Seems to be a bug, sounds awfully lot like this: https://bugreports.qt.io/browse/QTBUG-5123
Upvotes: 1