AnsweredAssumed Answered

Bug: Cpp Example - Global Object with new/malloc in Construct

Question asked by Rui Faria on Sep 11, 2013
Latest reply on Nov 28, 2013 by Rui Faria

Hello!

 

I am using TWRK70 + MQX 4.0.1 + CW10.4 Windows XP + g++.

 

I created a new MQX 4.0 project by selecting a Example application named cplus and changed the "HelloWorld" Class to use operator new in Constructor methods of Class, see the changed in below as BOLD:

 

class HelloWorld {

private:

   int check_init;

   const char *id;

  

   void* pointer;

public:

   HelloWorld() {

      check_init = 0x1234567;

     

      pointer = new int;

   }

   ~HelloWorld() {

      _io_printf("%s: deallocation\n",id);

     

      delete (int*)pointer;

      pointer = NULL;

   }

   void print(const char *x) {

      id = x;

      if (check_init == 0x1234567) {

         _io_printf("%s: Constructed OK\n",id);

      } else {

         _io_printf("%s: Constructor not called\n",id);

      }

   }

};

 

And it is created a Global Object with the name "global"

 

static HelloWorld global;

 

Then Crashed!!

 

I noticed that the C++ static initializers is called before of MQX initialization and the allocation of HEAP memory.

 

My quick fix was:

 

  • comment the following line in __thumb_startup function, in __arm_start.c source file in ARM_GCC_Support\ewl\EWL_Runtime\src\arm folder and recompile it.

 

          //__call_static_initializers();

 

  • added the following lines in init_bsp.c source file in mqx\source\bsp\twrk70f120m folder

 

          #if BSPCFG_ENABLE_CPP

          extern void __init_cpp(void);

          #endif

 

  • added the following lines in _bsp_enable_card function, in init_bsp.c source file in mqx\source\bsp\twrk70f120m folder

 

          #if BSPCFG_ENABLE_CPP

             /* initialize C++ constructors */    

              __init_cpp();

          #endif

 

  • recompile MQX Kernel

 

Seems to work properly but i don't know if it is missing some detail. Anyone can help me?

 

Thanks.

Outcomes