Qt 4.8
Public Types | Public Functions | Related Functions | List of all members
QSharedMemory Class Reference

The QSharedMemory class provides access to a shared memory segment. More...

#include <qsharedmemory.h>

Inheritance diagram for QSharedMemory:
QObject

Public Types

enum  AccessMode { ReadOnly, ReadWrite }
 
enum  SharedMemoryError {
  NoError, PermissionDenied, InvalidSize, KeyError,
  AlreadyExists, NotFound, LockError, OutOfResources,
  UnknownError
}
 

Public Functions

bool attach (AccessMode mode=ReadWrite)
 Attempts to attach the process to the shared memory segment identified by the key that was passed to the constructor or to a call to setKey() or setNativeKey(). More...
 
const void * constData () const
 Returns a const pointer to the contents of the shared memory segment, if one is attached. More...
 
bool create (int size, AccessMode mode=ReadWrite)
 Creates a shared memory segment of size bytes with the key passed to the constructor, set with setKey() or set with setNativeKey(), then attaches to the new shared memory segment with the given access mode and returns true. More...
 
void * data ()
 Returns a pointer to the contents of the shared memory segment, if one is attached. More...
 
const void * data () const
 
bool detach ()
 Detaches the process from the shared memory segment. More...
 
SharedMemoryError error () const
 Returns a value indicating whether an error occurred, and, if so, which error it was. More...
 
QString errorString () const
 Returns a text description of the last error that occurred. More...
 
bool isAttached () const
 Returns true if this process is attached to the shared memory segment. More...
 
QString key () const
 Returns the key assigned with setKey() to this shared memory, or a null key if no key has been assigned, or if the segment is using a nativeKey(). More...
 
bool lock ()
 This is a semaphore that locks the shared memory segment for access by this process and returns true. More...
 
QString nativeKey () const
 Returns the native, platform specific, key for this shared memory object. More...
 
 QSharedMemory (QObject *parent=0)
 
 QSharedMemory (const QString &key, QObject *parent=0)
 Constructs a shared memory object with the given parent and with its key set to key. More...
 
void setKey (const QString &key)
 Sets the platform independent key for this shared memory object. More...
 
void setNativeKey (const QString &key)
 Sets the native, platform specific, key for this shared memory object. More...
 
int size () const
 Returns the size of the attached shared memory segment. More...
 
bool unlock ()
 Releases the lock on the shared memory segment and returns true, if the lock is currently held by this process. More...
 
 ~QSharedMemory ()
 The destructor clears the key, which forces the shared memory object to detach() {detach} from its underlying shared memory segment. More...
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Related Functions

(Note that these are not member functions.)

 QSharedMemory
 Constructs a shared memory object with the given parent. More...
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

The QSharedMemory class provides access to a shared memory segment.

Since
4.4

QSharedMemory provides access to a shared memory segment by multiple threads and processes. It also provides a way for a single thread or process to lock the memory for exclusive access.

When using this class, be aware of the following platform differences:

Remember to lock the shared memory with lock() before reading from or writing to the shared memory, and remember to release the lock with unlock() after you are done.

Unlike QtSharedMemory, QSharedMemory automatically destroys the shared memory segment when the last instance of QSharedMemory is detached from the segment, and no references to the segment remain. Do not mix using QtSharedMemory and QSharedMemory. Port everything to QSharedMemory.

Warning
QSharedMemory changes the key in a Qt-specific way, unless otherwise specified. Interoperation with non-Qt applications is achieved by first creating a default shared memory with QSharedMemory() and then setting a native key with setNativeKey(). When using native keys, shared memory is not protected against multiple accesses on it (e.g. unable to lock()) and a user-defined mechanism should be used to achieve a such protection.

Definition at line 57 of file qsharedmemory.h.

Enumerations

◆ AccessMode

  • ReadOnly The shared memory segment is read-only. Writing to the shared memory segment is not allowed. An attempt to write to a shared memory segment created with ReadOnly causes the program to abort.
  • ReadWrite Reading and writing the shared memory segment are both allowed.
Enumerator
ReadOnly 
ReadWrite 

Definition at line 63 of file qsharedmemory.h.

◆ SharedMemoryError

  • NoError No error occurred.
  • PermissionDenied The operation failed because the caller didn't have the required permissions.
  • InvalidSize A create operation failed because the requested size was invalid.
  • KeyError The operation failed because of an invalid key.
  • AlreadyExists A create() operation failed because a shared memory segment with the specified key already existed.
  • NotFound An attach() failed because a shared memory segment with the specified key could not be found.
  • OutOfResources A create() operation failed because there was not enough memory available to fill the request.
  • UnknownError Something else happened and it was bad.
Enumerator
NoError 
PermissionDenied 
InvalidSize 
KeyError 
AlreadyExists 
NotFound 
LockError 
OutOfResources 
UnknownError 

Definition at line 69 of file qsharedmemory.h.

Constructors and Destructors

◆ QSharedMemory() [1/2]

QSharedMemory::QSharedMemory ( QObject parent = 0)

Definition at line 177 of file qsharedmemory.cpp.

178  : QObject(*new QSharedMemoryPrivate, parent)
179 {
180 }
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
Definition: qobject.cpp:753

◆ QSharedMemory() [2/2]

QSharedMemory::QSharedMemory ( const QString key,
QObject parent = 0 
)

Constructs a shared memory object with the given parent and with its key set to key.

Because its key is set, its create() and attach() functions can be called.

See also
setKey(), create(), attach()

Definition at line 189 of file qsharedmemory.cpp.

190  : QObject(*new QSharedMemoryPrivate, parent)
191 {
192  setKey(key);
193 }
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
Definition: qobject.cpp:753
void setKey(const QString &key)
Sets the platform independent key for this shared memory object.

◆ ~QSharedMemory()

QSharedMemory::~QSharedMemory ( )

The destructor clears the key, which forces the shared memory object to detach() {detach} from its underlying shared memory segment.

If this shared memory object is the last one connected to the shared memory segment, the detach() operation destroys the shared memory segment.

See also
detach() isAttached()

Definition at line 204 of file qsharedmemory.cpp.

205 {
206  setKey(QString());
207 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
void setKey(const QString &key)
Sets the platform independent key for this shared memory object.

Functions

◆ attach()

bool QSharedMemory::attach ( AccessMode  mode = ReadWrite)

Attempts to attach the process to the shared memory segment identified by the key that was passed to the constructor or to a call to setKey() or setNativeKey().

The access mode is QSharedMemory::ReadWrite by default. It can also be QSharedMemory::ReadOnly . Returns true if the attach operation is successful. If false is returned, call error() to determine which error occurred. After attaching the shared memory segment, a pointer to the shared memory can be obtained by calling data().

See also
isAttached(), detach(), create()

Definition at line 425 of file qsharedmemory.cpp.

Referenced by QVNCScreenPrivate::configure().

426 {
428 
429  if (isAttached() || !d->initKey())
430  return false;
431 #ifndef QT_NO_SYSTEMSEMAPHORE
433  if (!d->key.isNull() && !d->tryLocker(&lock, QLatin1String("QSharedMemory::attach")))
434  return false;
435 #endif
436 
437  if (isAttached() || !d->handle())
438  return false;
439 
440  return d->attach(mode);
441 }
double d
Definition: qnumeric_p.h:62
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
#define Q_D(Class)
Definition: qglobal.h:2482
bool lock()
This is a semaphore that locks the shared memory segment for access by this process and returns true...
bool isAttached() const
Returns true if this process is attached to the shared memory segment.
The QSharedMemory class provides access to a shared memory segment.
Definition: qsharedmemory.h:57
Helper class.

◆ constData()

const void * QSharedMemory::constData ( ) const

Returns a const pointer to the contents of the shared memory segment, if one is attached.

Otherwise it returns null. Remember to lock the shared memory with lock() before reading from or writing to the shared memory, and remember to release the lock with unlock() after you are done.

See also
attach() create()

Definition at line 504 of file qsharedmemory.cpp.

505 {
506  Q_D(const QSharedMemory);
507  return d->memory;
508 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QSharedMemory class provides access to a shared memory segment.
Definition: qsharedmemory.h:57

◆ create()

bool QSharedMemory::create ( int  size,
AccessMode  mode = ReadWrite 
)

Creates a shared memory segment of size bytes with the key passed to the constructor, set with setKey() or set with setNativeKey(), then attaches to the new shared memory segment with the given access mode and returns true.

If a shared memory segment identified by the key already exists, the attach operation is not performed and false is returned. When the return value is false, call error() to determine which error occurred.

See also
error()

Definition at line 358 of file qsharedmemory.cpp.

Referenced by QVNCScreenPrivate::configure().

359 {
361 
362  if (!d->initKey())
363  return false;
364 
365  if (size <= 0) {
366  d->error = QSharedMemory::InvalidSize;
367  d->errorString = QSharedMemory::tr("%1: create size is less then 0").arg(QLatin1String("QSharedMemory::create"));
368  return false;
369  }
370 
371 #ifndef QT_NO_SYSTEMSEMAPHORE
372 #ifndef Q_OS_WIN
373  // Take ownership and force set initialValue because the semaphore
374  // might have already existed from a previous crash.
375  d->systemSemaphore.setKey(d->key, 1, QSystemSemaphore::Create);
376 #endif
377 
379  if (!d->key.isNull() && !d->tryLocker(&lock, QLatin1String("QSharedMemory::create")))
380  return false;
381 #endif
382 
383  if (!d->create(size))
384  return false;
385 
386  return d->attach(mode);
387 }
double d
Definition: qnumeric_p.h:62
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
#define Q_D(Class)
Definition: qglobal.h:2482
bool lock()
This is a semaphore that locks the shared memory segment for access by this process and returns true...
int size() const
Returns the size of the attached shared memory segment.
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
The QSharedMemory class provides access to a shared memory segment.
Definition: qsharedmemory.h:57
Helper class.

◆ data() [1/2]

void * QSharedMemory::data ( )

Returns a pointer to the contents of the shared memory segment, if one is attached.

Otherwise it returns null. Remember to lock the shared memory with lock() before reading from or writing to the shared memory, and remember to release the lock with unlock() after you are done.

See also
attach()

Definition at line 489 of file qsharedmemory.cpp.

Referenced by QVNCScreenPrivate::configure().

490 {
492  return d->memory;
493 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QSharedMemory class provides access to a shared memory segment.
Definition: qsharedmemory.h:57

◆ data() [2/2]

const void * QSharedMemory::data ( ) const

Definition at line 513 of file qsharedmemory.cpp.

514 {
515  Q_D(const QSharedMemory);
516  return d->memory;
517 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QSharedMemory class provides access to a shared memory segment.
Definition: qsharedmemory.h:57

◆ detach()

bool QSharedMemory::detach ( )

Detaches the process from the shared memory segment.

If this was the last process attached to the shared memory segment, then the shared memory segment is released by the system, i.e., the contents are destroyed. The function returns true if it detaches the shared memory segment. If it returns false, it usually means the segment either isn't attached, or it is locked by another process.

See also
attach(), isAttached()

Definition at line 465 of file qsharedmemory.cpp.

Referenced by QVNCScreenPrivate::configure(), QVNCScreen::disconnect(), setKey(), setNativeKey(), and QVNCScreenPrivate::~QVNCScreenPrivate().

466 {
468  if (!isAttached())
469  return false;
470 
471 #ifndef QT_NO_SYSTEMSEMAPHORE
473  if (!d->key.isNull() && !d->tryLocker(&lock, QLatin1String("QSharedMemory::detach")))
474  return false;
475 #endif
476 
477  return d->detach();
478 }
double d
Definition: qnumeric_p.h:62
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
#define Q_D(Class)
Definition: qglobal.h:2482
bool lock()
This is a semaphore that locks the shared memory segment for access by this process and returns true...
bool isAttached() const
Returns true if this process is attached to the shared memory segment.
The QSharedMemory class provides access to a shared memory segment.
Definition: qsharedmemory.h:57
Helper class.

◆ error()

QSharedMemory::SharedMemoryError QSharedMemory::error ( ) const

Returns a value indicating whether an error occurred, and, if so, which error it was.

See also
errorString()

Definition at line 606 of file qsharedmemory.cpp.

Referenced by QSharedMemoryPrivate::initKey().

607 {
608  Q_D(const QSharedMemory);
609  return d->error;
610 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QSharedMemory class provides access to a shared memory segment.
Definition: qsharedmemory.h:57

◆ errorString()

QString QSharedMemory::errorString ( ) const

Returns a text description of the last error that occurred.

If error() returns an QSharedMemory::SharedMemoryError {error value}, call this function to get a text string that describes the error.

See also
error()

Definition at line 620 of file qsharedmemory.cpp.

Referenced by QVNCScreenPrivate::configure(), and QSharedMemoryPrivate::initKey().

621 {
622  Q_D(const QSharedMemory);
623  return d->errorString;
624 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QSharedMemory class provides access to a shared memory segment.
Definition: qsharedmemory.h:57

◆ isAttached()

bool QSharedMemory::isAttached ( ) const

Returns true if this process is attached to the shared memory segment.

See also
attach(), detach()

Definition at line 449 of file qsharedmemory.cpp.

Referenced by attach(), detach(), setKey(), and setNativeKey().

450 {
451  Q_D(const QSharedMemory);
452  return (0 != d->memory);
453 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QSharedMemory class provides access to a shared memory segment.
Definition: qsharedmemory.h:57

◆ key()

QString QSharedMemory::key ( ) const

Returns the key assigned with setKey() to this shared memory, or a null key if no key has been assigned, or if the segment is using a nativeKey().

The key is the identifier used by Qt applications to identify the shared memory segment.

You can find the native, platform specific, key used by the operating system by calling nativeKey().

See also
setKey() setNativeKey()

Definition at line 321 of file qsharedmemory.cpp.

Referenced by QSharedMemoryPrivate::initKey(), setKey(), and setNativeKey().

322 {
323  Q_D(const QSharedMemory);
324  return d->key;
325 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QSharedMemory class provides access to a shared memory segment.
Definition: qsharedmemory.h:57

◆ lock()

bool QSharedMemory::lock ( )

This is a semaphore that locks the shared memory segment for access by this process and returns true.

If another process has locked the segment, this function blocks until the lock is released. Then it acquires the lock and returns true. If this function returns false, it means that you have ignored a false return from create() or attach(), that you have set the key with setNativeKey() or that QSystemSemaphore::acquire() failed due to an unknown system error.

See also
unlock(), data(), QSystemSemaphore::acquire()

Definition at line 531 of file qsharedmemory.cpp.

Referenced by attach(), create(), and detach().

532 {
534  if (d->lockedByMe) {
535  qWarning("QSharedMemory::lock: already locked");
536  return true;
537  }
538  if (d->systemSemaphore.acquire()) {
539  d->lockedByMe = true;
540  return true;
541  }
542  QString function = QLatin1String("QSharedMemory::lock");
543  d->errorString = QSharedMemory::tr("%1: unable to lock").arg(function);
544  d->error = QSharedMemory::LockError;
545  return false;
546 }
double d
Definition: qnumeric_p.h:62
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_D(Class)
Definition: qglobal.h:2482
Q_CORE_EXPORT void qWarning(const char *,...)
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
The QSharedMemory class provides access to a shared memory segment.
Definition: qsharedmemory.h:57

◆ nativeKey()

QString QSharedMemory::nativeKey ( ) const

Returns the native, platform specific, key for this shared memory object.

Since
4.8

The native key is the identifier used by the operating system to identify the shared memory segment.

You can use the native key to access shared memory segments that have not been created by Qt, or to grant shared memory access to non-Qt applications.

See also
setKey() setNativeKey()

Definition at line 342 of file qsharedmemory.cpp.

343 {
344  Q_D(const QSharedMemory);
345  return d->nativeKey;
346 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QSharedMemory class provides access to a shared memory segment.
Definition: qsharedmemory.h:57

◆ setKey()

void QSharedMemory::setKey ( const QString key)

Sets the platform independent key for this shared memory object.

If key is the same as the current key, the function returns without doing anything.

You can call key() to retrieve the platform independent key. Internally, QSharedMemory converts this key into a platform specific key. If you instead call nativeKey(), you will get the platform specific, converted key.

If the shared memory object is attached to an underlying shared memory segment, it will detach() {detach} from it before setting the new key. This function does not do an attach().

See also
key() nativeKey() isAttached()

Definition at line 223 of file qsharedmemory.cpp.

Referenced by QVNCScreenPrivate::configure(), QSharedMemory(), and ~QSharedMemory().

224 {
226  if (key == d->key && d->makePlatformSafeKey(key) == d->nativeKey)
227  return;
228 
229  if (isAttached())
230  detach();
231  d->cleanHandle();
232  d->key = key;
233  d->nativeKey = d->makePlatformSafeKey(key);
234 }
double d
Definition: qnumeric_p.h:62
bool detach()
Detaches the process from the shared memory segment.
#define Q_D(Class)
Definition: qglobal.h:2482
bool isAttached() const
Returns true if this process is attached to the shared memory segment.
The QSharedMemory class provides access to a shared memory segment.
Definition: qsharedmemory.h:57
QString key() const
Returns the key assigned with setKey() to this shared memory, or a null key if no key has been assign...

◆ setNativeKey()

void QSharedMemory::setNativeKey ( const QString key)

Sets the native, platform specific, key for this shared memory object.

Since
4.8

If key is the same as the current native key, the function returns without doing anything. If all you want is to assign a key to a segment, you should call setKey() instead.

You can call nativeKey() to retrieve the native key. If a native key has been assigned, calling key() will return a null string.

If the shared memory object is attached to an underlying shared memory segment, it will detach() {detach} from it before setting the new key. This function does not do an attach().

The application will not be portable if you set a native key.

See also
nativeKey() key() isAttached()

Definition at line 258 of file qsharedmemory.cpp.

259 {
261  if (key == d->nativeKey && d->key.isNull())
262  return;
263 
264  if (isAttached())
265  detach();
266  d->cleanHandle();
267  d->key.clear();
268  d->nativeKey = key;
269 }
double d
Definition: qnumeric_p.h:62
bool detach()
Detaches the process from the shared memory segment.
#define Q_D(Class)
Definition: qglobal.h:2482
bool isAttached() const
Returns true if this process is attached to the shared memory segment.
The QSharedMemory class provides access to a shared memory segment.
Definition: qsharedmemory.h:57
QString key() const
Returns the key assigned with setKey() to this shared memory, or a null key if no key has been assign...

◆ size()

int QSharedMemory::size ( ) const

Returns the size of the attached shared memory segment.

If no shared memory segment is attached, 0 is returned.

See also
create() attach()

Definition at line 395 of file qsharedmemory.cpp.

Referenced by QVNCScreenPrivate::configure().

396 {
397  Q_D(const QSharedMemory);
398  return d->size;
399 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QSharedMemory class provides access to a shared memory segment.
Definition: qsharedmemory.h:57

◆ unlock()

bool QSharedMemory::unlock ( )

Releases the lock on the shared memory segment and returns true, if the lock is currently held by this process.

If the segment is not locked, or if the lock is held by another process, nothing happens and false is returned.

See also
lock()

Definition at line 556 of file qsharedmemory.cpp.

557 {
559  if (!d->lockedByMe)
560  return false;
561  d->lockedByMe = false;
562  if (d->systemSemaphore.release())
563  return true;
564  QString function = QLatin1String("QSharedMemory::unlock");
565  d->errorString = QSharedMemory::tr("%1: unable to unlock").arg(function);
566  d->error = QSharedMemory::LockError;
567  return false;
568 }
double d
Definition: qnumeric_p.h:62
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_D(Class)
Definition: qglobal.h:2482
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
The QSharedMemory class provides access to a shared memory segment.
Definition: qsharedmemory.h:57

Friends and Related Functions

◆ QSharedMemory()

QSharedMemory ( )
related

Constructs a shared memory object with the given parent.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

The shared memory object's key is not set by the constructor, so the shared memory object does not have an underlying shared memory segment attached. The key must be set with setKey() or setNativeKey() before create() or attach() can be used.

See also
setKey()

The documentation for this class was generated from the following files: