From bd0d03761ddda38dbd7ae34a29c904811b6f9683 Mon Sep 17 00:00:00 2001 From: Christian Heimlich Date: Sat, 22 Jul 2023 23:24:12 -0400 Subject: [PATCH] Define AbstractError self-registration variable out-of-line to avoid multiple init When compiling the library as a DLL, since the variable within each derived error type was defined inline, multiple links to the Qx library in a project would cause there to be multiple versions of said variable, causing the class to register itself multiple times. --- lib/core/include/qx/core/qx-abstracterror.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/core/include/qx/core/qx-abstracterror.h b/lib/core/include/qx/core/qx-abstracterror.h index 74430652..7a586bed 100644 --- a/lib/core/include/qx/core/qx-abstracterror.h +++ b/lib/core/include/qx/core/qx-abstracterror.h @@ -91,7 +91,7 @@ friend class Error; static constexpr QLatin1StringView TYPE_NAME{EName.value}; private: - static inline const bool REGISTER = registerType(TYPE_CODE, TYPE_NAME); + static const bool REGISTER; //-Constructor------------------------------------------------------------------------------------------------------------- protected: @@ -126,6 +126,15 @@ friend class Error; // [](AbstractError&){}(type); //}; +/* Define error type registrar variable. This must be done out of line to ensure that only + * one instance of the variable exists per-error-type across an entire program. If the variable + * is defined inline, multiple versiosn of it can exist in parallel when linking via shared-libraries, + * if those libraries are used by multiple targets in the same project. This would cause an error type + * to call registerType() multiple times. + */ +template +const bool AbstractError::REGISTER = registerType(TYPE_CODE, TYPE_NAME); + namespace AbstractErrorPrivate { template