From c255ca2959e77ec5e3b6b0eddafb69ee3ef356b6 Mon Sep 17 00:00:00 2001 From: Erik Olieman <> Date: Wed, 11 Sep 2013 16:58:34 +0300 Subject: [PATCH] USBDevice memory leaks fixes Fixes memory leaks in USBMSD, USBSerial (CircBuffer) and the KL25Z USB HAL. Original author: Erik Olieman, a few changes by Bogdan Marinescu. --- .../USBDevice/USBDevice/USBHAL_KL25Z.cpp | 26 ++++++++++++++----- libraries/USBDevice/USBMSD/USBMSD.cpp | 11 ++++++++ libraries/USBDevice/USBMSD/USBMSD.h | 9 +++++++ libraries/USBDevice/USBSerial/CircBuffer.h | 4 +++ 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/libraries/USBDevice/USBDevice/USBHAL_KL25Z.cpp b/libraries/USBDevice/USBDevice/USBHAL_KL25Z.cpp index d01f80ccc4..9afa2a980b 100644 --- a/libraries/USBDevice/USBDevice/USBHAL_KL25Z.cpp +++ b/libraries/USBDevice/USBDevice/USBHAL_KL25Z.cpp @@ -169,6 +169,16 @@ void USBHAL::disconnect(void) { USB0->CTL &= ~USB_CTL_USBENSOFEN_MASK; // Pull up disable USB0->CONTROL &= ~USB_CONTROL_DPPULLUPNONOTG_MASK; + + //Free buffers if required: + for (int i = 0; i<(NUMBER_OF_PHYSICAL_ENDPOINTS - 2) * 2; i++) { + free(endpoint_buffer[i]); + endpoint_buffer[i] = NULL; + } + free(endpoint_buffer_iso[2]); + endpoint_buffer_iso[2] = NULL; + free(endpoint_buffer_iso[0]); + endpoint_buffer_iso[0] = NULL; } void USBHAL::configureDevice(void) { @@ -200,18 +210,22 @@ bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t flag if ((flags & ISOCHRONOUS) == 0) { handshake_flag = USB_ENDPT_EPHSHK_MASK; if (IN_EP(endpoint)) { - endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD )] = (uint8_t *) malloc (64*2); - buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD )][0]; + if (endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)] == NULL) + endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)] = (uint8_t *) malloc (64*2); + buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)][0]; } else { - endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD )] = (uint8_t *) malloc (64*2); - buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD )][0]; + if (endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)] == NULL) + endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)] = (uint8_t *) malloc (64*2); + buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)][0]; } } else { if (IN_EP(endpoint)) { - endpoint_buffer_iso[2] = (uint8_t *) malloc (1023*2); + if (endpoint_buffer_iso[2] == NULL) + endpoint_buffer_iso[2] = (uint8_t *) malloc (1023*2); buf = &endpoint_buffer_iso[2][0]; } else { - endpoint_buffer_iso[0] = (uint8_t *) malloc (1023*2); + if (endpoint_buffer_iso[0] == NULL) + endpoint_buffer_iso[0] = (uint8_t *) malloc (1023*2); buf = &endpoint_buffer_iso[0][0]; } } diff --git a/libraries/USBDevice/USBMSD/USBMSD.cpp b/libraries/USBDevice/USBMSD/USBMSD.cpp index f1a932bee7..3bda241bdb 100644 --- a/libraries/USBDevice/USBMSD/USBMSD.cpp +++ b/libraries/USBDevice/USBMSD/USBMSD.cpp @@ -67,8 +67,12 @@ USBMSD::USBMSD(uint16_t vendor_id, uint16_t product_id, uint16_t product_release stage = READ_CBW; memset((void *)&cbw, 0, sizeof(CBW)); memset((void *)&csw, 0, sizeof(CSW)); + page = NULL; } +USBMSD::~USBMSD() { + disconnect(); +} // Called in ISR context to process a class specific request @@ -117,6 +121,7 @@ bool USBMSD::connect() { if (BlockCount > 0) { BlockSize = MemorySize / BlockCount; if (BlockSize != 0) { + free(page); page = (uint8_t *)malloc(BlockSize * sizeof(uint8_t)); if (page == NULL) return false; @@ -130,6 +135,12 @@ bool USBMSD::connect() { return true; } +void USBMSD::disconnect() { + //De-allocate MSD page size: + free(page); + page = NULL; + USBDevice::disconnect(); +} void USBMSD::reset() { stage = READ_CBW; diff --git a/libraries/USBDevice/USBMSD/USBMSD.h b/libraries/USBDevice/USBMSD/USBMSD.h index 0516054ffc..ba852fa96e 100644 --- a/libraries/USBDevice/USBMSD/USBMSD.h +++ b/libraries/USBDevice/USBMSD/USBMSD.h @@ -74,6 +74,15 @@ public: */ bool connect(); + /** + * Disconnect the USB MSD device. + */ + void disconnect(); + + /** + * Destructor + */ + ~USBMSD(); protected: diff --git a/libraries/USBDevice/USBSerial/CircBuffer.h b/libraries/USBDevice/USBSerial/CircBuffer.h index 54226ec54f..78d5eecc6c 100644 --- a/libraries/USBDevice/USBSerial/CircBuffer.h +++ b/libraries/USBDevice/USBSerial/CircBuffer.h @@ -29,6 +29,10 @@ public: buf = (T *)malloc(size * sizeof(T)); }; + ~CircBuffer() { + free(buf); + } + bool isFull() { return ((write + 1) % size == read); };