cancel
Showing results for 
Search instead for 
Did you mean: 

Allocated memory corrupted in Interruptroutine?

TheaArt
Associate III

Hello Community,

I'm developing a program for a STM32 L051R6T6 with CubeIDE.on bare metal, using HAL driver functions.

It reads some cyclic data from two USARTS using DMA.

The maincycle does processing of the rxTelegrams, transmits and does some logging on a third LPUART.

I now I'm experiencing problems with longer logstrings. They are allocated from heap with malloc with necessary size (500 Bytes in my case and checked for NULL). But while the maincycle fills it with logging content, a DMA Interrupt occures and process the DMAData from one of the USARTs. In the Interrupt routine a function is called and when I step with the debugger from this function header to the first instruction, my allocated logbuffer from the maincycle is overwritten with 24 Bytes near the end.

My questions are:

Shouldn't the allocated memory from the maincycle also be protected from overwriting in Interrupt routines? If not: how could I avoid such behavior.

What is done, when I jump from the function header to the first instruction in debugger. Is this the creation step of the values of function parameters ( the content of the 24 Bytes doesnt look like the parameters, as far as i can tell).

Of course there may be memory leaks in the program, but in this case all seems to be valid.

Thank you.

4 REPLIES 4
Bubbles
ST Employee

Hi @Wolfgang Rißler​ ,

from your description it almost looks like a textbook example of an overrun problem. Try looking to the map file produced by the linker if you see something suspicious.

You mention memory protection. There is no memory protection by default, it has to be configured and enabled. The MPU can be used to guard memory boundaries. It throws an interrupt and you can observe whrere exactly the problem occurs.

BR,

J

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

gbm
Lead III

Rule #1: do not use dynamic memory allocation for simple task on small microcontrollers. You won't gain anything and get some troubles instead.

TheaArt
Associate III

Hello and thank you for your replys,

I know, that heap allocation is generally not a good idea. It's the only place, where I do this, to save memory. and performance. (So I dont need to copy the logstrings).

It looks like the RAM is on its limit and so I shortened the quite long logmessages. So its seems to work for the moment.

Also when I experimented with long messages as a test, I got jumps to the ErrorHandler. But I was wondering, that overwriting allocated memoryareas (like described above) could happen, without ErrorHandler exceptions.

Is there a easy way to monitor the memory usage? Something like HAL_Get_Free_Memory_Amount()?

Determine what is causing it to enter ErrorHandle(), suggest using the source file and line reporting variant to speed diagnosis.

P​erhaps look at the heap allocation functions in the C library, or simply walk the heap structures which are typically a linked list.

Perhaps wrap malloc() free() to track use and misuse.

A​void using heap functions in interrupt or callback context.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..