From 9af0ccfa890852a3f5749ba944e50dca36fe47ad Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 6 Nov 2024 17:53:48 +0100 Subject: [PATCH] Core: Generate a static random number in Application::applicationPid() Using Qt's QCoreApplication::applicationPid() doesn't always give a unique ID so that multiple FreeCAD instances cannot be executed at the same time. This fixes #17678 --- src/App/Application.cpp | 14 +++++++++++++- src/App/PreCompiled.h | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/App/Application.cpp b/src/App/Application.cpp index 31df8e4e3178..9384790627da 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -35,6 +35,8 @@ # endif # include # include +# include +# include #endif #ifdef FC_OS_WIN32 @@ -1117,7 +1119,17 @@ Application::TransactionSignaller::~TransactionSignaller() { int64_t Application::applicationPid() { - return QCoreApplication::applicationPid(); + static int64_t randomNumber = []() { + auto tp = std::chrono::high_resolution_clock::now(); + auto dur = tp.time_since_epoch(); + auto seed = dur.count(); + std::mt19937 generator(static_cast(seed)); + constexpr int64_t minValue {1}; + constexpr int64_t maxValue {1000000}; + std::uniform_int_distribution distribution(minValue, maxValue); + return distribution(generator); + }(); + return randomNumber; } std::string Application::getHomePath() diff --git a/src/App/PreCompiled.h b/src/App/PreCompiled.h index 86389849a57d..b421ee048b11 100644 --- a/src/App/PreCompiled.h +++ b/src/App/PreCompiled.h @@ -69,6 +69,7 @@ // STL #include +#include #include #include #include