Skip to content

Commit

Permalink
Define AbstractError self-registration variable out-of-line to avoid
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
oblivioncth committed Jul 23, 2023
1 parent 6812df5 commit bd0d037
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/core/include/qx/core/qx-abstracterror.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -126,6 +126,15 @@ friend class Error;
// []<StringLiteral Y, quint16 Z>(AbstractError<Y, Z>&){}(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<StringLiteral EName, quint16 ECode>
const bool AbstractError<EName, ECode>::REGISTER = registerType(TYPE_CODE, TYPE_NAME);

namespace AbstractErrorPrivate
{
template<Qx::StringLiteral Y, quint16 Z>
Expand Down

0 comments on commit bd0d037

Please sign in to comment.