Our embedded system is built on a hw/sw platform made by enea. After the platform updated recently, we found some operations on the global variable keep crashing the system.
For example, we have a global map structure holding some data. We can insert/iterate the map once or twice, then the address of the elements in the map suddenly changed to some forbidden addresses like 0x0 or 0x1d, the system just crash.
The only different before/after the platform update is :
1) sw part: It’s a c++ software and We changed the compiler from diab cc to gcc.
2) hw part: we have a new board, but the cpu is still powerpc405s.
I tried every possible way but still can’t figure out the reason. Any thoughts?
A known issue with globals is order of initialization. This order is generally not defined. As a result, you may see crashes if the ctor of one global tries to use another global. In your case, the problem may be that GCC has decided to initialize the map later, after the point where you’re using it.
A quick solution can be to replace the global with a singleton:
singletonis created before the function returns, and therefore certainly before it’s used.