From 11e2ba1f9267adf30a78a81b4c861840b0b8a1f3 Mon Sep 17 00:00:00 2001 From: Russ Butler Date: Tue, 1 May 2018 15:05:17 -0500 Subject: [PATCH] Add USBDevice destructor Add a destructor to USBDevice to ensure that resources have been properly released. Additionally add an assert in the destructor that deinit has already been called. If it has not been called then interrupts can still occur which may cause a crash. --- usb/device/USBDevice/USBDevice.cpp | 6 ++++++ usb/device/USBDevice/USBDevice.h | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/usb/device/USBDevice/USBDevice.cpp b/usb/device/USBDevice/USBDevice.cpp index 0788358b21..3c73170c4b 100644 --- a/usb/device/USBDevice/USBDevice.cpp +++ b/usb/device/USBDevice/USBDevice.cpp @@ -1248,6 +1248,12 @@ USBDevice::USBDevice(USBPhy *phy, uint16_t vendor_id, uint16_t product_id, uint1 _device.suspended = false; } +USBDevice::~USBDevice() +{ + MBED_ASSERT(!_initialized); + deinit(); +} + uint32_t USBDevice::endpoint_max_packet_size(usb_ep_t endpoint) { lock(); diff --git a/usb/device/USBDevice/USBDevice.h b/usb/device/USBDevice/USBDevice.h index d5a91f1b26..a1b491574b 100644 --- a/usb/device/USBDevice/USBDevice.h +++ b/usb/device/USBDevice/USBDevice.h @@ -82,6 +82,14 @@ public: */ USBDevice(USBPhy *phy, uint16_t vendor_id, uint16_t product_id, uint16_t product_release); + /** + * Cleanup this USBDevice + * + * This USBDevice must be uninitialized when the destructor is + * called or the behavior is undefined. + */ + virtual ~USBDevice(); + /** * Initialize this instance *