Skip to content

Commit c255ca2

Browse files
Erik OliemanBogdan Marinescu
authored andcommitted
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.
1 parent 94ff741 commit c255ca2

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

libraries/USBDevice/USBDevice/USBHAL_KL25Z.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ void USBHAL::disconnect(void) {
169169
USB0->CTL &= ~USB_CTL_USBENSOFEN_MASK;
170170
// Pull up disable
171171
USB0->CONTROL &= ~USB_CONTROL_DPPULLUPNONOTG_MASK;
172+
173+
//Free buffers if required:
174+
for (int i = 0; i<(NUMBER_OF_PHYSICAL_ENDPOINTS - 2) * 2; i++) {
175+
free(endpoint_buffer[i]);
176+
endpoint_buffer[i] = NULL;
177+
}
178+
free(endpoint_buffer_iso[2]);
179+
endpoint_buffer_iso[2] = NULL;
180+
free(endpoint_buffer_iso[0]);
181+
endpoint_buffer_iso[0] = NULL;
172182
}
173183

174184
void USBHAL::configureDevice(void) {
@@ -200,18 +210,22 @@ bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t flag
200210
if ((flags & ISOCHRONOUS) == 0) {
201211
handshake_flag = USB_ENDPT_EPHSHK_MASK;
202212
if (IN_EP(endpoint)) {
203-
endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD )] = (uint8_t *) malloc (64*2);
204-
buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD )][0];
213+
if (endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)] == NULL)
214+
endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)] = (uint8_t *) malloc (64*2);
215+
buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)][0];
205216
} else {
206-
endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD )] = (uint8_t *) malloc (64*2);
207-
buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD )][0];
217+
if (endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)] == NULL)
218+
endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)] = (uint8_t *) malloc (64*2);
219+
buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)][0];
208220
}
209221
} else {
210222
if (IN_EP(endpoint)) {
211-
endpoint_buffer_iso[2] = (uint8_t *) malloc (1023*2);
223+
if (endpoint_buffer_iso[2] == NULL)
224+
endpoint_buffer_iso[2] = (uint8_t *) malloc (1023*2);
212225
buf = &endpoint_buffer_iso[2][0];
213226
} else {
214-
endpoint_buffer_iso[0] = (uint8_t *) malloc (1023*2);
227+
if (endpoint_buffer_iso[0] == NULL)
228+
endpoint_buffer_iso[0] = (uint8_t *) malloc (1023*2);
215229
buf = &endpoint_buffer_iso[0][0];
216230
}
217231
}

libraries/USBDevice/USBMSD/USBMSD.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ USBMSD::USBMSD(uint16_t vendor_id, uint16_t product_id, uint16_t product_release
6767
stage = READ_CBW;
6868
memset((void *)&cbw, 0, sizeof(CBW));
6969
memset((void *)&csw, 0, sizeof(CSW));
70+
page = NULL;
7071
}
7172

73+
USBMSD::~USBMSD() {
74+
disconnect();
75+
}
7276

7377

7478
// Called in ISR context to process a class specific request
@@ -117,6 +121,7 @@ bool USBMSD::connect() {
117121
if (BlockCount > 0) {
118122
BlockSize = MemorySize / BlockCount;
119123
if (BlockSize != 0) {
124+
free(page);
120125
page = (uint8_t *)malloc(BlockSize * sizeof(uint8_t));
121126
if (page == NULL)
122127
return false;
@@ -130,6 +135,12 @@ bool USBMSD::connect() {
130135
return true;
131136
}
132137

138+
void USBMSD::disconnect() {
139+
//De-allocate MSD page size:
140+
free(page);
141+
page = NULL;
142+
USBDevice::disconnect();
143+
}
133144

134145
void USBMSD::reset() {
135146
stage = READ_CBW;

libraries/USBDevice/USBMSD/USBMSD.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ class USBMSD: public USBDevice {
7474
*/
7575
bool connect();
7676

77+
/**
78+
* Disconnect the USB MSD device.
79+
*/
80+
void disconnect();
81+
82+
/**
83+
* Destructor
84+
*/
85+
~USBMSD();
7786

7887
protected:
7988

libraries/USBDevice/USBSerial/CircBuffer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class CircBuffer {
2929
buf = (T *)malloc(size * sizeof(T));
3030
};
3131

32+
~CircBuffer() {
33+
free(buf);
34+
}
35+
3236
bool isFull() {
3337
return ((write + 1) % size == read);
3438
};

0 commit comments

Comments
 (0)