tags:

views:

51

answers:

3

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: 

It seems like you have a out-of-memory condition, or a defect memory allocator. When you use a stl container, you can change the allocator as template parameter to malloc_alloc and use a lib like dmalloc to see if the memory house holding goes wrong (see http://www.sgi.com/tech/stl/Allocators.html). You can also try to compile the software under linux and use valgrind to look for memory problems.

Rudi
A: 

Some idea:

  • By changing the compiler, the cross platform macro or the compiler predefined macro may have change.
  • Does the driver of the new board to use the hardware or else, been included / updated in the new software. The CPU may be the same but not for the rest of the hardware in your board. (if it is used)
  • Problem of memory allocation: (memory size of the system not the same, overflow of the stack/heap)
  • Uninitialized data for example may have a different behavior in two different compiler/platform.
  • Use of undefined behavior source code (which have different implementation depending on the compiler)
Phong
+2  A: 

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:

MyClass& MyClass::instance() {
  static MyClass singleton;
  return singleton;
}

singleton is created before the funtion returns, and therefore certainly before it's used.

MSalters
Thanks a lot, MSalters. I will try this solution as soon as possible.But can this explain that we can operate on the global data structure once or twice before it crashes?