SingleApplication: Use new QSharedMemory constructor with Qt 6.6 and higher

This commit is contained in:
Jonas Kvinge 2023-03-02 22:57:33 +01:00
parent e22199817c
commit 0adc084dad
2 changed files with 18 additions and 0 deletions

View File

@ -39,6 +39,7 @@
#include <QThread>
#include <QSharedMemory>
#include <QLocalSocket>
#include <QNativeIpcKey>
#include <QByteArray>
#include <QElapsedTimer>
#include <QtDebug>
@ -71,13 +72,21 @@ SingleApplication::SingleApplication(int &argc, char *argv[], const bool allowSe
#ifdef Q_OS_UNIX
// By explicitly attaching it and then deleting it we make sure that the memory is deleted even after the process has crashed on Unix.
#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
d->memory_ = new QSharedMemory(QNativeIpcKey(d->blockServerName_));
#else
d->memory_ = new QSharedMemory(d->blockServerName_);
#endif
d->memory_->attach();
delete d->memory_;
#endif
// Guarantee thread safe behaviour with a shared memory block.
#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
d->memory_ = new QSharedMemory(QNativeIpcKey(d->blockServerName_));
#else
d->memory_ = new QSharedMemory(d->blockServerName_);
#endif
// Create a shared memory block
if (d->memory_->create(sizeof(InstancesInfo))) {

View File

@ -39,6 +39,7 @@
#include <QThread>
#include <QSharedMemory>
#include <QLocalSocket>
#include <QNativeIpcKey>
#include <QByteArray>
#include <QElapsedTimer>
#include <QtDebug>
@ -71,13 +72,21 @@ SingleCoreApplication::SingleCoreApplication(int &argc, char *argv[], const bool
#ifdef Q_OS_UNIX
// By explicitly attaching it and then deleting it we make sure that the memory is deleted even after the process has crashed on Unix.
#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
d->memory_ = new QSharedMemory(QNativeIpcKey(d->blockServerName_));
#else
d->memory_ = new QSharedMemory(d->blockServerName_);
#endif
d->memory_->attach();
delete d->memory_;
#endif
// Guarantee thread safe behaviour with a shared memory block.
#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
d->memory_ = new QSharedMemory(QNativeIpcKey(d->blockServerName_));
#else
d->memory_ = new QSharedMemory(d->blockServerName_);
#endif
// Create a shared memory block
if (d->memory_->create(sizeof(InstancesInfo))) {