As I’ve said before (in previous posts about much the same, and it is a continuing problem) I have a relatively complex system. STM32 microprocessor (doesn’t matter which), external memory (QSPI through the OctoSPI interface, ends up memory mapped).
Programming is a C++ overlay calling C support routines (CubeMXIDE) and FreeRTOS.
ALL system programming is in C++ structures (occasional virtual routines, mostly for displays), Calls to hal routines (ST provided drivers) are surrounded by code, generally protected by a semaphore (and not the STMicro routines, apparently a big difference).
ST Micro’s main.c routine sets up a task then calls that task. Call to the task never returns, so the default task is executing. Default task is used to call a C++ routine, which initializes a variety of subsystems (registry, packet, display drivers, HAL drivers (mine, not theirs), etc.
The C++ routine then calls another routine which sets up the main application task, then returns (back to the C++ routine which then returns back to the default task. Default task is never used again, and is fitted with a 10 second delay.
Problem: adding additional data arrays to a task called from the C++ routine crashes the system. This is in a task (not pointer derived, but set up for an instance of a variable). Expanding that variable (by adding data, and it was another class) caused the crash.
Symptoms: Execute program, go into expanded task, then change a variable. Changing that variable causes other data in that program to change. (possibly related to what the variable was). It wipes out other programming in the system.
So I have working (previously) code that adding data to causes a hard fault, because data within the program for that item is corrupted.
I think I know what the problem is, but my reasoning involves guessing. It’s how C++ memory management and C (FreeRTOS memory management work). The ultimate answer is that they don’t talk to each other.
FreeRTOS: heap 4, lots and lots of memory (256K). Program heap trimmed so that malloc failed hook does not happen (requires modification to malloc part of program). Therefore, heap is ok.
Now, looking at C++ classes. I’m not sure where that memory is allocated (mixed C, C++ program). What is seems like is that the C++ classes have no idea of what FreeRtos is doing, so you get pointers to a C++ area, and pointers to a FreeRTOS area. Adding data to a C++ class expands the class, but FreeRTOS (and possibly the linker) have no idea of what’s going on.
The solution to this problem is to find the class that is causing the problem, then change that instance to a pointer, not compile time assigned variable.
If the problem is in registry, which is declared as an instance of REGISTRY, then registry needs to be changed to a pointer, NEW is used, and the program works.
I’m likely missing something, but this is a recurring problem, even though I do have a fix (yeah, change it all to pointers).
FreeRTOS has its own memory area in off chip memory, the memory manager is heap4, and some data areas are directed through another memory manager (mine) to a third memory area.
I’m still puzzled.
I use NEW for the memory assignments to classes, CubeMXIDE has thread protection, I certainly shouldn’t be running out of memory, but I still suspect that memory assigned and needed in C++ classes (even normal class variables) are not picked up by either the compiler or FreeRTOS.
It is a puzzlement.