-
Notifications
You must be signed in to change notification settings - Fork 7.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unclear documentation: Is malloc task/threadsafe? (IDFGH-1502) #3768
Comments
No, you do not. It's guaranteed by the language (C & C++), assuming you're using a modern version of it, (C11 ?). I have a hard time seeing you running into issues if you use a tool chain provided by Espressif though.
|
Thank you I didn't know that was the case as the memory functions are written by expressif. |
esp-idf/components/heap/multi_heap_platform.h Lines 22 to 35 in 81ca1c0
|
Thank you. So is malloc safe in ISRs? |
Ah, seems an ISR is allowed to call malloc too, that's nice. |
So the documentation is unclear? |
@raldone01 malloc(), free(), and other heap functions are thread safe. It is technically possible to malloc() or free() from an ISR. It is not recommended to call malloc() or free() from an ISR as they can take a long time to run, especially if you have PSRAM with a lot of small allocations in it. Better to structure your code in a way that you pre-allocate any buffer that is needed by the ISR. |
Will reopen as I've made a clarification in the docs about this, but it hasn't pushed to GItHub yet. This issue will be closed once it has. |
I carefully read through https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/mem_alloc.html but I couldn't find anything regarding task/thread safety.
Do I have to guard malloc and free calls from concurrent access?
I would suggest to update the documentation and add that info.
The text was updated successfully, but these errors were encountered: