Skip to content

Commit 44eeebb

Browse files
authored
cores/rp2040: _freertos.cpp: Avoid memory leak and unnecessary allocation (#948)
There's a memory leak around the corner: creating the mutex means that we allocate memory for it, but if we cannot find any free slot in the FMMap array, we're simply returning a nullptr without ever freeing the previously allocated memory. Instead of allocating it out of the free-slot check, do it inside. While at it, also check if the mutex creation succeeded before assigning it to a FMMap slot.
1 parent ec76310 commit 44eeebb

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

cores/rp2040/_freertos.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,15 @@ extern "C" SemaphoreHandle_t __get_freertos_mutex_for_ptr(mutex_t *m) {
3838
return _map[i].dst;
3939
}
4040
}
41-
// Make a new mutex
42-
auto fm = __freertos_mutex_create();
41+
4342
for (int i = 0; i < 16; i++) {
4443
if (_map[i].src == nullptr) {
44+
// Make a new mutex
45+
SemaphoreHandle_t fm = __freertos_mutex_create();
46+
if (fm == nullptr) {
47+
return nullptr;
48+
}
49+
4550
_map[i].src = m;
4651
_map[i].dst = fm;
4752
return fm;

0 commit comments

Comments
 (0)