Skip to content

Commit ccc013e

Browse files
Omkar-DarekarOmkar Darekaritay-grudev
authored
Fix QSharedMemory on macOS with Qt 6.6+ (#215)
Qt 6.6+ defaults to POSIX Realtime shared memory via QNativeIpcKey on macOS. However, POSIX shared memory (shm_open) requires Qt to be built with -feature-ipc_posix and has strict naming requirements. Many Qt installations (including Homebrew) don't have POSIX IPC enabled, causing the error: QSharedMemory::KeyError "QSharedMemory::attach (shm_open): bad name" Use legacyNativeKey() to force System V shared memory, which works reliably on all macOS configurations regardless of Qt build options. Fixes single instance detection failing on macOS with Qt 6.6+. Co-authored-by: Omkar Darekar <Omkar.Darekar@veritas.com> Co-authored-by: Itay Grudev <itay+git2020@grudev.com>
1 parent b117282 commit ccc013e

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

singleapplication.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda
9696
// By explicitly attaching it and then deleting it we make sure that the
9797
// memory is deleted even after the process has crashed on Unix.
9898
#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
99-
d->memory = new QSharedMemory( QNativeIpcKey( d->blockServerName ) );
99+
//d->memory = new QSharedMemory( QNativeIpcKey( d->blockServerName ) ); //old implementation
100+
// Use legacy (System V) key type as POSIX realtime shm may not work on macOS
101+
d->memory = new QSharedMemory( QSharedMemory::legacyNativeKey( d->blockServerName ) );
100102
#else
101103
d->memory = new QSharedMemory( d->blockServerName );
102104
#endif
@@ -105,7 +107,9 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda
105107
#endif
106108
// Guarantee thread safe behaviour with a shared memory block.
107109
#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
108-
d->memory = new QSharedMemory( QNativeIpcKey( d->blockServerName ) );
110+
//d->memory = new QSharedMemory( QNativeIpcKey( d->blockServerName ) ); //old implementation
111+
// Use legacy (System V) key type as POSIX realtime shm may not work on macOS
112+
d->memory = new QSharedMemory( QSharedMemory::legacyNativeKey( d->blockServerName ) );
109113
#else
110114
d->memory = new QSharedMemory( d->blockServerName );
111115
#endif

0 commit comments

Comments
 (0)