QtBase  v6.3.1
Public Types | Public Member 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:
Inheritance graph
[legend]
Collaboration diagram for QSharedMemory:
Collaboration graph
[legend]

Public Types

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

Public Member Functions

 QSharedMemory (QObject *parent=nullptr)
 
 QSharedMemory (const QString &key, QObject *parent=nullptr)
 
 ~QSharedMemory ()
 
void setKey (const QString &key)
 
QString key () const
 
void setNativeKey (const QString &key)
 
QString nativeKey () const
 
bool create (qsizetype size, AccessMode mode=ReadWrite)
 
qsizetype size () const
 
bool attach (AccessMode mode=ReadWrite)
 
bool isAttached () const
 
bool detach ()
 
voiddata ()
 
const voidconstData () const
 
const voiddata () const
 
bool lock ()
 
bool unlock ()
 
SharedMemoryError error () const
 
QString errorString () const
 
- Public Member Functions inherited from QObject
Q_INVOKABLE QObject (QObject *parent=nullptr)
 
virtual ~QObject ()
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
QString objectName () const
 
void setObjectName (const QString &name)
 
QBindable< QStringbindableObjectName ()
 
bool isWidgetType () const
 
bool isWindowType () const
 
bool signalsBlocked () const noexcept
 
bool blockSignals (bool b) noexcept
 
QThreadthread () const
 
void moveToThread (QThread *thread)
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
void killTimer (int id)
 
template<typename T >
T findChild (const QString &aName=QString(), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<typename T >
QList< TfindChildren (const QString &aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<typename T >
QList< TfindChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
const QObjectListchildren () const
 
void setParent (QObject *parent)
 
void installEventFilter (QObject *filterObj)
 
void removeEventFilter (QObject *obj)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=nullptr, const QObject *receiver=nullptr, const char *member=nullptr) const
 
bool disconnect (const QObject *receiver, const char *member=nullptr) const
 
void dumpObjectTree () const
 
void dumpObjectInfo () const
 
bool setProperty (const char *name, const QVariant &value)
 
QVariant property (const char *name) const
 
QList< QByteArraydynamicPropertyNames () const
 
QBindingStoragebindingStorage ()
 
const QBindingStoragebindingStorage () const
 
QObjectparent () const
 
bool inherits (const char *classname) const
 

Related Functions

(Note that these are not member functions.)

 QSharedMemory
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 
- Signals inherited from QObject
void destroyed (QObject *=nullptr)
 
void objectNameChanged (const QString &objectName, QPrivateSignal)
 
- Static Public Member Functions inherited from QObject
static QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 
static QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
 
static QMetaObject::Connection ::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 slot)
 
static QMetaObject::Connection ::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *context, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static std::enable_if< QtPrivate::FunctionPointer< Func2 >::ArgumentCount==-1 &&!std::is_convertible_v< Func2, const char * >, QMetaObject::Connection >::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 slot)
 
template<typename Func1 , typename Func2 >
static std::enable_if< QtPrivate::FunctionPointer< Func2 >::ArgumentCount==-1 &&!std::is_convertible_v< Func2, const char * >, QMetaObject::Connection >::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *context, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static bool disconnect (const QMetaObject::Connection &)
 
template<typename Func1 , typename Func2 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot)
 
template<typename Func1 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero)
 
- Protected Member Functions inherited from QObject
QObjectsender () const
 
int senderSignalIndex () const
 
int receivers (const char *signal) const
 
bool isSignalConnected (const QMetaMethod &signal) const
 
virtual void timerEvent (QTimerEvent *event)
 
virtual void childEvent (QChildEvent *event)
 
virtual void customEvent (QEvent *event)
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
 QObject (QObjectPrivate &dd, QObject *parent=nullptr)
 
- Protected Attributes inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Properties inherited from QObject
QString objectName
 the name of this object More...
 

Detailed Description

The QSharedMemory class provides access to a shared memory segment.

\inmodule QtCore

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:

\list

\list

QSharedMemory shm("DEVTEAMID.app-group/shared");
if (!shm.create(42) && shm.error() == QSharedMemory::AlreadyExists)
shm.attach();
The QSharedMemory class provides access to a shared memory segment.
Definition: qsharedmemory.h:62

\endlist

\endlist

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.

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.

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 (for example, unable to lock()) and a user-defined mechanism should be used to achieve such protection.

Definition at line 58 of file qsharedmemory.h.

Member Enumeration Documentation

◆ AccessMode

\value 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.

\value ReadWrite Reading and writing the shared memory segment are both allowed.

Enumerator
ReadOnly 
ReadWrite 

Definition at line 69 of file qsharedmemory.h.

◆ SharedMemoryError

\value NoError No error occurred.

\value PermissionDenied The operation failed because the caller didn't have the required permissions.

\value InvalidSize A create operation failed because the requested size was invalid.

\value KeyError The operation failed because of an invalid key.

\value AlreadyExists A create() operation failed because a shared memory segment with the specified key already existed.

\value NotFound An attach() failed because a shared memory segment with the specified key could not be found.

\value LockError The attempt to lock() the shared memory segment failed because create() or attach() failed and returned false, or because a system error occurred in QSystemSemaphore::acquire().

\value OutOfResources A create() operation failed because there was not enough memory available to fill the request.

\value UnknownError Something else happened and it was bad.

Enumerator
NoError 
PermissionDenied 
InvalidSize 
KeyError 
AlreadyExists 
NotFound 
LockError 
OutOfResources 
UnknownError 

Definition at line 75 of file qsharedmemory.h.

Constructor & Destructor Documentation

◆ QSharedMemory() [1/2]

QSharedMemory::QSharedMemory ( QObject parent = nullptr)

Definition at line 212 of file qsharedmemory.cpp.

◆ QSharedMemory() [2/2]

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

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 230 of file qsharedmemory.cpp.

Here is the call graph for this function:

◆ ~QSharedMemory()

QSharedMemory::~QSharedMemory ( )

The destructor clears the key, which forces the shared memory object to \l {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 252 of file qsharedmemory.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ 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 \l {QSharedMemory::} {ReadWrite} by default. It can also be \l {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 474 of file qsharedmemory.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 553 of file qsharedmemory.cpp.

Here is the caller graph for this function:

◆ create()

bool QSharedMemory::create ( qsizetype  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 \tt true. If a shared memory segment identified by the key already exists, the attach operation is not performed and \tt false is returned. When the return value is \tt false, call error() to determine which error occurred.

See also
error()

Definition at line 400 of file qsharedmemory.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 538 of file qsharedmemory.cpp.

Here is the caller graph for this function:

◆ data() [2/2]

const void * QSharedMemory::data ( ) const

Definition at line 562 of file qsharedmemory.cpp.

Here is the caller graph for this function:

◆ 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 514 of file qsharedmemory.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 655 of file qsharedmemory.cpp.

Here is the caller graph for this function:

◆ errorString()

QString QSharedMemory::errorString ( ) const

Returns a text description of the last error that occurred. If error() returns an \l {QSharedMemory::SharedMemoryError} {error value}, call this function to get a text string that describes the error.

See also
error()

Definition at line 669 of file qsharedmemory.cpp.

◆ isAttached()

bool QSharedMemory::isAttached ( ) const

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

See also
attach(), detach()

Definition at line 498 of file qsharedmemory.cpp.

Here is the caller graph for this function:

◆ 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 366 of file qsharedmemory.cpp.

Here is the caller graph for this function:

◆ 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 580 of file qsharedmemory.cpp.

Here is the caller graph for this function:

◆ nativeKey()

QString QSharedMemory::nativeKey ( ) const
Since
4.8

Returns the native, platform specific, key for this shared memory object. 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 384 of file qsharedmemory.cpp.

◆ 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 \l {detach()} {detach} from it before setting the new key. This function does not do an attach().

See also
key(), nativeKey(), isAttached()

Definition at line 271 of file qsharedmemory.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setNativeKey()

void QSharedMemory::setNativeKey ( const QString key)
Since
4.8

Sets the native, platform specific, key for this shared memory object. 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 \l {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 303 of file qsharedmemory.cpp.

Here is the call graph for this function:

◆ size()

qsizetype QSharedMemory::size ( ) const

Returns the size of the attached shared memory segment. If no shared memory segment is attached, 0 is returned.

Note
The size of the segment may be larger than the requested size that was passed to create().
See also
create(), attach()

Definition at line 444 of file qsharedmemory.cpp.

Here is the caller graph for this function:

◆ 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 605 of file qsharedmemory.cpp.

Here is the caller graph for this function:

Friends And Related Function Documentation

◆ QSharedMemory()

QSharedMemory ( )
related

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

Constructs a shared memory object with the given parent. 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: