PDA

View Full Version : shared memory wont attach



daemonna
16th July 2010, 23:05
here is my code:



QSharedMemory numofallplayers;

void sesman::run()
{
numofallplayers.setKey("numofallplayers");
numofallplayers.create(sizeof(qint64), QSharedMemory::ReadWrite);

qDebug() << "session manager thread starts from" << QThread::currentThread();
qDebug() << "seting total players to zero";
sesman::SetNumOfAllPlayers(0);
qDebug() << "num of players total: " << sesman::GetNumOfAllPlayers(); //just to check
.
.
.
cleanup();
}


void sesman::SetNumOfAllPlayers(qint64 totalplayers)
{
if(numofallplayers.isAttached()){
numofallplayers.detach();
}
if(!numofallplayers.attach(QSharedMemory::ReadWrit e)){
//some error
qDebug() << "attach numofallplayers failed" << numofallplayers.errorString();
}
numofallplayers.lock();
qint64* totalpl = (qint64*)numofallplayers.data();
*totalpl = totalplayers;
numofallplayers.unlock();
if(!numofallplayers.detach())
qDebug() << "unable to detach";
}


and i get following error

seting total players to zero
attach numofallplayers failed "QSharedMemory::handle:: UNIX key file doesn't exist"
The program has unexpectedly finished.

Can someone help? what is wrong with my code?

thanks a lot for any help...

tbscope
17th July 2010, 09:52
Can you check if the following line succeeded?


numofallplayers.create(sizeof(qint64), QSharedMemory::ReadWrite);

daemonna
25th August 2010, 01:11
problem wasn't in shared memory, but in a threading I'm using... so now I know I have to call sharedmemory.create in beginning of every thread, so i make sure it's created before any thread attach it... and in threading you never know which thread will be first :)