Skip to content

Commit f4dfac8

Browse files
committed
Always call new DoubleBufferedEPOutHandler on reconnect
Avoid memory leak by deleting the buffers; DoubleBufferedEPOutHandler should be refactored (in beta branch) to allow separate "new" and "setup" procedures Fixes #350
1 parent a8eec3b commit f4dfac8

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

cores/arduino/USB/SAMD21_USBDevice.h

+5
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ class DoubleBufferedEPOutHandler : public EPHandler {
254254
release();
255255
}
256256

257+
~DoubleBufferedEPOutHandler() {
258+
free((void*)data0);
259+
free((void*)data1);
260+
}
261+
257262
virtual uint32_t recv(void *_data, uint32_t len)
258263
{
259264
uint8_t *data = reinterpret_cast<uint8_t *>(_data);

cores/arduino/USB/USBCore.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ uint8_t udd_ep_in_cache_buffer[7][64];
9191
// Some EP are handled using EPHanlders.
9292
// Possibly all the sparse EP handling subroutines will be
9393
// converted into reusable EPHandlers in the future.
94-
static EPHandler *epHandlers[7];
94+
static EPHandler *epHandlers[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
9595

9696
//==================================================================
9797

@@ -454,9 +454,10 @@ void USBDeviceClass::initEP(uint32_t ep, uint32_t config)
454454
}
455455
else if (config == (USB_ENDPOINT_TYPE_BULK | USB_ENDPOINT_OUT(0)))
456456
{
457-
if (epHandlers[ep] == NULL) {
458-
epHandlers[ep] = new DoubleBufferedEPOutHandler(usbd, ep, 256);
457+
if (epHandlers[ep] != NULL) {
458+
delete (DoubleBufferedEPOutHandler*)epHandlers[ep];
459459
}
460+
epHandlers[ep] = new DoubleBufferedEPOutHandler(usbd, ep, 256);
460461
}
461462
else if (config == (USB_ENDPOINT_TYPE_BULK | USB_ENDPOINT_IN(0)))
462463
{

0 commit comments

Comments
 (0)