Sure that the concurrent access to malloc() calls from multiple threads is a problem that should be addressed. However the main problem looks to be related to how malloc understands the RAM. From my testing, if I place...
Hi Joe! The reply I am referring is the one where I end with Does this code also works for you? I stopped using printf as it seems to cause many problems and in my full code I have already started to use the...
That's correct. While I agree that it would be possible to just use malloc from the main thread, it would require drastic changes in my code because Strings are used. And sometimes when they grow in size, realloc() is...
That's another very interesting library I will definitively check. Once again, thanks Joe. I am already using the arduino-printf library which was your suggestion and so far it fits my code like a glove. Barely had to...
Defragster, I tried to run the code with the modifications you mentioned in your last post (#48) and could also reproduce the behavior you have shown. Very weird. I have no idea what caused this.
That's...
Ok, so I was thinking about this:
And came up with an crazy ideia that (at least in my testing) worked: What if all the threads stacks were above the top of the heap, like it is natural for a stack to be?
What I...
I have tested all the codes posted previously but haven't had success in being able to reliably malloc() from any thread at any time, without needing to malloc() from the main thread in order to "unlock" the malloc to...
I don't understand. What do you mean by "nothing magic"? In my testing, if I comment out the preliminary allocation from the main thread, it doesn't work. If I let it there, it works. Isn't it related to the first...
One other thing I tested was to sometimes free the heap space allocated by the threads and check if new allocations were able to use the empty spaces left on the heap. As far as I could see, it works. So I think malloc...
Hello defragster and joepasquariello!
I have been experimenting with your codes and making some small modifications to try to isolate the problem and find the root cause. Thanks very much for your help so far! :)
I...
Thanks again, defragster. I will test many allocs and frees from different threads and check the pointers to see where the memory is being allocated in the ram.
You are correct! That's the exact same behavior I was noticing. In my case I have tested with only the main thread + 1 created thread. Sometimes the thread 1 is unable to allocate memory, but if the main thread tries to...
Yes, TeensyThreads uses "new", which maps directly to malloc(). But there is also the option to provide an already existing buffer to serve as stack for the newly created thread. In my full code, I am already doing...
Please correct me if I am wrong, but my understanding is: the heap and the main thread stack are placed at opposite ends of the memory. The heap grows up and the stack grows down. We don't even know if the stack and...
That's a very interesting take! So a "quick and dirty" workaround for the issue would be to allocate "X" bytes of RAM using malloc in the main thread before adding any new thread, which will initialize some internal...
Exactly! I have performed some of those tests myself and also couldn't find any clear relationship between the stack size of the threads and how many bytes could be allocated using malloc() inside the thread before...
Mjs513, that's correct. The issue of memory leakage, calling malloc() without calling free() would crash the sketch anyway under any circumstance. My ideia was just to show that malloc() fails after I allocated around 5...
Thanks Joe! I will definitely give this library a try.. I think the whole code will benefit from it. Unfortunately, I will still need to use malloc() or new for things like Strings and objetc constructors, so the...
HI Mark, thanks for the answer. I don't have access to the code during the weekend, but I didn't change any compilation flags or settings, they are all default. I used platformIO and later installed a fresh copy of the...
I was playing around with the code and was able to actually make the program crash with a much simpler code:
#include <Arduino.h>
#include "TeensyThreads.h"
void thread_func()
{
while(1)
{
Hello all!
I am recently started using the TeensyThreads library in order to have two different tasks working concurrently. Everything was working fine until I started using dynamic memory allocation using malloc,...