Closed
Description
Describe the bug
When calling begin() after the receive and transmit buffers have been allocated, the begin function orphans the allocations by resetting the pointer to nullptr, on the next call of allocateTxBuffer(), we get a new memory allocation while the previous one has never been freed, this in turns causes heap segmentation and will lead to lack of memory in the long run.
Proposed fix in #2442
To Reproduce
Using this simplified code, we can see that the Wire receive buffer is reallocated every requestFrom call
#include <Arduino.h>
#include <Wire.h>
void setup() {
}
void loop() {
Wire.begin();
Wire.requestFrom(1,1);
}
Steps to reproduce the behavior:
- Run demo code
- Place a breakpoint in allocateRxBuffer right after the realloc line
- Notice the tmp pointer increase location every call
- Wait long enough and you'll run out of memory
Expected behavior
The wire library should not cause a memory leak, even if calling begin() multiple times is bad practice.
Board (please complete the following information):
- Name: BlackPill STM32F411CEU6
- Hardware Revision: V3.1
Applicable to any stm32
Metadata
Metadata
Assignees
Labels
No labels