From 8cab209ef91cbda34bae8afa880f152c4f1473b0 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Thu, 1 Oct 2015 17:35:26 +0200 Subject: [PUSB] Fix static initialization order fiasco For details see: https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use --- libraries/HID/HID.cpp | 8 ++++++-- libraries/HID/HID.h | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'libraries/HID') diff --git a/libraries/HID/HID.cpp b/libraries/HID/HID.cpp index b7aab6c..ff8e67b 100644 --- a/libraries/HID/HID.cpp +++ b/libraries/HID/HID.cpp @@ -21,7 +21,11 @@ #if defined(USBCON) -HID_ HID; +HID_& HID() +{ + static HID_ obj; + return obj; +} int HID_::getInterface(uint8_t* interfaceNum) { @@ -113,7 +117,7 @@ HID_::HID_(void) : PUSBListNode(1, 1, epType), protocol(1), idle(1) { epType[0] = EP_TYPE_INTERRUPT_IN; - PluggableUSB.plug(this); + PluggableUSB().plug(this); } int HID_::begin(void) diff --git a/libraries/HID/HID.h b/libraries/HID/HID.h index 2cd0f4a..a7f5c68 100644 --- a/libraries/HID/HID.h +++ b/libraries/HID/HID.h @@ -93,6 +93,11 @@ private: uint8_t idle; }; +// Replacement for global singleton. +// This function prevents static-initialization-order-fiasco +// https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use +HID_& HID(); + #define D_HIDREPORT(length) { 9, 0x21, 0x01, 0x01, 0, 1, 0x22, lowByte(length), highByte(length) } #endif // USBCON -- cgit v1.2.3-18-g5258