PDA

View Full Version : QSharedMemory and QThreads



valerianst
30th April 2014, 09:52
Goodmorning everyone,

I'm developing a multithreaded application that uses a class that inherits from QSharedMemory.

Since some time ago I was using classes that extended QThread that opens (creates if it hasn't been created or attaches in the other case) the shared memory object in the constructor and close it in the destructor.
I had sometimes problems when destroying the threads so I decided to switch to the moveToThread suggested approach. Using this last, it seems that I've get rid of the old problems but I've got new ones..

I'm using the deleteLater approach and now sometimes I get this error: "unable to set key on lock" in my constructor and other times, in the destructor it remains blocked on the detach. I read the QSharedMemory code ,including the Linux part (that I'm using), in order to understand how it works. It seems that it uses locks in the attach and detach so it seems like a deadlock problem.

Does anyone know what that error mean?



Cheers!

sulliwk06
30th April 2014, 14:16
Looks like it could be 1 of a few things. I think you'll have to look at the actual error code to get a more specific idea on what is going wrong.

bool QSharedMemoryPrivate::initKey()
{
if (!cleanHandle())
return false;
#ifndef QT_NO_SYSTEMSEMAPHORE
systemSemaphore.setKey(QString(), 1);
systemSemaphore.setKey(key, 1);
if (systemSemaphore.error() != QSystemSemaphore::NoError) {
QString function = QLatin1String("QSharedMemoryPrivate::initKey");
errorString = QSharedMemory::tr("%1: unable to set key on lock").arg(function);
switch(systemSemaphore.error()) {
case QSystemSemaphore::PermissionDenied:
error = QSharedMemory::PermissionDenied;
break;
case QSystemSemaphore::KeyError:
error = QSharedMemory::KeyError;
break;
case QSystemSemaphore::AlreadyExists:
error = QSharedMemory::AlreadyExists;
break;
case QSystemSemaphore::NotFound:
error = QSharedMemory::NotFound;
break;
case QSystemSemaphore::OutOfResources:
error = QSharedMemory::OutOfResources;
break;
case QSystemSemaphore::UnknownError:
default:
error = QSharedMemory::UnknownError;
break;
}
return false;
}
#endif
errorString = QString();
error = QSharedMemory::NoError;
return true;
}

valerianst
30th April 2014, 14:36
Hi there,

yep I saw that code but in order to get the specific error code I need to do some changes. I guess I'll try to get it.


The specifi message error I get is: "QSharedMemoryPrivate::initKey: unable to set key on lock"

valerianst
30th April 2014, 17:30
The error code is: QSharedMemory::KeyError 3 The operation failed because of an invalid key.


I'm wondering what can it be.. the key is the same in all the threads but it should work in this way

Djoni_D
9th October 2015, 13:18
Hi, I'am Djon. Sorry for bad english, i'am from Russia.

The problem is that the list is full of stored keys. This error occurs when you have created a large number of copies QHaredMemory members with keys.