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 --- cores/arduino/PluggableUSB.cpp | 8 ++++++-- cores/arduino/PluggableUSB.h | 5 ++++- cores/arduino/USBCore.cpp | 6 +++--- 3 files changed, 13 insertions(+), 6 deletions(-) (limited to 'cores') diff --git a/cores/arduino/PluggableUSB.cpp b/cores/arduino/PluggableUSB.cpp index acc6276..582379a 100644 --- a/cores/arduino/PluggableUSB.cpp +++ b/cores/arduino/PluggableUSB.cpp @@ -25,8 +25,6 @@ extern uint8_t _initEndpoints[]; -PluggableUSB_ PluggableUSB; - int PluggableUSB_::getInterface(uint8_t* interfaceNum) { int sent = 0; @@ -90,6 +88,12 @@ bool PluggableUSB_::plug(PUSBListNode *node) // restart USB layer??? } +PluggableUSB_& PluggableUSB() +{ + static PluggableUSB_ obj; + return obj; +} + PluggableUSB_::PluggableUSB_() : lastIf(CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT), lastEp(CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT), rootNode(NULL) diff --git a/cores/arduino/PluggableUSB.h b/cores/arduino/PluggableUSB.h index 824440a..b0668f4 100644 --- a/cores/arduino/PluggableUSB.h +++ b/cores/arduino/PluggableUSB.h @@ -66,7 +66,10 @@ private: PUSBListNode* rootNode; }; -extern PluggableUSB_ PluggableUSB; +// Replacement for global singleton. +// This function prevents static-initialization-order-fiasco +// https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use +PluggableUSB_& PluggableUSB(); #endif diff --git a/cores/arduino/USBCore.cpp b/cores/arduino/USBCore.cpp index ebdce36..5db9f52 100644 --- a/cores/arduino/USBCore.cpp +++ b/cores/arduino/USBCore.cpp @@ -362,7 +362,7 @@ bool ClassInterfaceRequest(USBSetup& setup) return CDC_Setup(setup); #ifdef PLUGGABLE_USB_ENABLED - return PluggableUSB.setup(setup, i); + return PluggableUSB().setup(setup, i); #endif return false; } @@ -440,7 +440,7 @@ static u8 SendInterfaces() CDC_GetInterface(&interfaces); #ifdef PLUGGABLE_USB_ENABLED - PluggableUSB.getInterface(&interfaces); + PluggableUSB().getInterface(&interfaces); #endif return interfaces; @@ -476,7 +476,7 @@ bool SendDescriptor(USBSetup& setup) InitControl(setup.wLength); #ifdef PLUGGABLE_USB_ENABLED - ret = PluggableUSB.getDescriptor(t); + ret = PluggableUSB().getDescriptor(t); if (ret != 0) { return (ret > 0 ? true : false); } -- cgit v1.2.3-18-g5258