From 3d459bf28d78348fa8547311fc4b70fc18f67ca2 Mon Sep 17 00:00:00 2001 From: XMuli Date: Sat, 4 Jan 2025 20:17:46 +0800 Subject: [PATCH] chore: Failed to create instance Sometimes Ubuntu20.04-24.04 execution of `d->memory->attach()` crashes here and prints the necessary information to determine that the creation has failed --- singleapplication.cpp | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/singleapplication.cpp b/singleapplication.cpp index 95c4d18..e7b7f03 100644 --- a/singleapplication.cpp +++ b/singleapplication.cpp @@ -72,8 +72,9 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda #else d->memory = new QSharedMemory( d->blockServerName ); #endif - d->memory->attach(); - delete d->memory; + bool ok = d->memory->attach(); + qCritical() << "d->memory->attach():" << ok; + if (ok) delete d->memory; #endif // Guarantee thread safe behaviour with a shared memory block. #if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0) @@ -91,16 +92,24 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda } d->initializeMemoryBlock(); } else { + + qCritical() << "Shared memory creation failed:" << d->memory->errorString(); + if( d->memory->error() == QSharedMemory::AlreadyExists ){ // Attempt to attach to the memory segment if( ! d->memory->attach() ){ qCritical() << "SingleApplication: Unable to attach to shared memory block."; abortSafely(); + } else { + qCritical() << "Successfully attached to existing shared memory block"; } if( ! d->memory->lock() ){ qCritical() << "SingleApplication: Unable to lock memory block after attach."; abortSafely(); + } else { + qCritical() << "Memory block successfully locked"; } + } else { qCritical() << "SingleApplication: Unable to create block."; abortSafely(); @@ -108,6 +117,21 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda } auto *inst = static_cast( d->memory->data() ); + + if (!inst) { + qCritical() << "Shared memory data is NULL!"; + abortSafely(); + } else { + + qCritical() << "Shared memory data doesn't NULL!"; + qCritical() << "Primary: " << inst->primary << QString::asprintf("0x%X", inst->primary); + qCritical() << "Secondary: " << inst->secondary << QString::asprintf("0x%X", inst->secondary); + qCritical() << "Primary PID: " << inst->primaryPid << QString::asprintf("0x%llX", inst->primaryPid); + qCritical() << "Checksum: " << inst->checksum << QString::asprintf("0x%X", inst->checksum); + qCritical() << "Primary User: " << inst->primaryUser; + } + + QElapsedTimer time; time.start();