Skip to content

Commit f43eb87

Browse files
authored
fix(task): Fix memory leak from task::run_on_core_nonblocking (#388)
* Remove call to `vTaskDelete(nullptr)` from the end of the thread function `vTaskDelete(nullptr)` was added as part of #353 based on spec. It was erroneously causing the detached thread to leak memory for each call to `run_on_core_nonblocking`. Using esp heap tracing I was able to determine that it did leak memory, while the updated code does not leak memory. Additionally, I used the `TaskMonitor` to ensure that the tasks are properly destroyed and are no longer registered within the system. * Build and run `task/example` on QtPy ESP32s3 and ensure it works still. * Build and run modified `task/example` on QtPy ESP32s3 which as freertos runtime stats (for task monitoring) and heap tracing enabled. Dump the heap trace output to ensure the residual memory allocated is not dependent on the number of times `run_on_core_nonblocking` was called. Confirmed that when including `vTaskDelay(nullptr)` the memory allocated (and leaked) is dependent on the number of times it is called.
1 parent 466011a commit f43eb87

File tree

1 file changed

+2
-8
lines changed

1 file changed

+2
-8
lines changed

components/task/include/run_on_core.hpp

+2-8
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,8 @@ static void run_on_core_non_blocking(const auto &f, int core_id, size_t stack_si
166166
f();
167167
return;
168168
}
169-
auto thread = std::thread(
170-
[](const auto &f) {
171-
f();
172-
// delete ourselves (the task that was created by the thread)
173-
vTaskDelete(nullptr);
174-
},
175-
f);
176-
thread.detach();
169+
std::thread t(f);
170+
t.detach();
177171
}
178172

179173
/// Run the given function on the specific core without blocking the calling thread

0 commit comments

Comments
 (0)