2020-09-25 08:31 AM
Hello,
I'm porting a C++ application to a STM32 device. The application itself is running, but I'm observing heap memory growing into the stack and firing an exception after a while. Running the application on Windows within Visual Studio shows the same behaviour regarding used memory going only up, but without crashing. This might be because there are GB of RAM instead of kB ;)
But I doubt that the application has memory leaks, I think that Windows is kidding me. The application is originally written on a Mac device. I'm in contact with the author and he couldn't see this behaviour on his setup and also never had crashes.
So, I assume I'm missing some compiler settings or something like that and I want to investigate the issue. I saw some articles regarding C++ on embedded devices and they recommend to overwrite the new and delete operator to use malloc() and free(). If I'd do this, I would also output a message how many memory is requested and freed. But I want to know the default implementation of the new and delete operators to check how they work. Where can I find those informations?
Thank you.
Regards
2020-09-29 02:16 AM
There is never negative increment to _sbrk.
Once a block is taken from _sbrk, it is managed by malloc.
Functions of the printf family use malloc ... So beware of recursivity.
2020-09-29 10:30 AM
@Nikita91
Documentation about _sbrk() states that it's possible that _sbrk() gets a negative value passed to it. So, your statement applies to newlib? I mean, in newlib it won't get a negative value?
You're right, I did not think of recursivity. For the printf function, I think I'll write a small one for the output I need. Since I only want to output decimal values, I think it should be possible without using dynamic memory.
@Uwe Bonnes
Since I want to port an existing C++ application, I've no choice :) And I'm hitting the limit. Now I'm trying to figure out why. I agree that dynamic memory should be used carefully, but this is also the case on a non-embedded system.
Regards
2020-10-04 05:34 AM
Where do you see the increment can be negative ?
Did you check the _sbrk implementation in sycalls.c to know if negative increment is properly managed ?
2020-10-04 08:54 AM
> For the printf function, I think I'll write a small one for the output I need.
Why printf?! You're using c++.
-- pa
2020-10-05 03:38 AM
@Nikita91
Where do you see the increment can be negative ?
https://sourceware.org/newlib/libc.html#malloc
The last section before the return value description states that it can be negative.
Did you check the _sbrk implementation in sycalls.c to know if negative increment is properly managed ?
Since the default _sbrk is only a stub which always returns an error, you've to implement your own. There are several implementations available for STM32, most of them only check if the heap is greater than the stack pointer. This is also the implementation I'm using. So, to answer the question: yes, I checked it, no, it doesn't :D But I've not seen a negative increment until now. So, I assume the ARM version of newlib indeed never decreases the heap.
@Pavel A.
Why printf?! You're using c++.
Why cin/cout? I'm using an embedded device, therefore printf() :D Using cin/cout links a lot of "dead" code statically, which isn't used. Waste of flash memory...
Regards