Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Failed to create instance #202

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions singleapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda
#else
d->memory = new QSharedMemory( d->blockServerName );
#endif
d->memory->attach();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When debugging and releasing, there is a high probability that this line will crash after a few more tries;

delete d->memory;
bool ok = d->memory->attach();
qCritical() << "d->memory->attach():" << ok;
if (ok) delete d->memory;
Comment on lines +75 to +77
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That first attach and delete are dummy operations. They are only there as I noticed that when there is orphaned shared memory after the original process has crashed, attaching and deleting clears it up. If it's not orphaned this is a no-op.

As such I didn't think I need to check their success status.

#endif
// Guarantee thread safe behaviour with a shared memory block.
#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
Expand All @@ -91,23 +92,46 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda
}
d->initializeMemoryBlock();
} else {

qCritical() << "Shared memory creation failed:" << d->memory->errorString();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a valid code path when there is already an instance running. There is no need for qCritical here.


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();
}
}

auto *inst = static_cast<InstancesInfo*>( 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();

Expand Down
Loading