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:
//__call_static_initializers();
#if BSPCFG_ENABLE_CPP
extern void __init_cpp(void);
#endif
#if BSPCFG_ENABLE_CPP
/* initialize C++ constructors */
__init_cpp();
#endif
Seems to work properly but i don't know if it is missing some detail. Anyone can help me?
Thanks.
Hi Rui,
This may be of some interest I had the same problem using IAR rather than CW and I had to change some linker options
--redirect __iar_dlmalloc=malloc
--redirect __iar_dlcalloc=calloc
--redirect __iar_dlfree=free
--skip_dynamic_initialization
And then, first thing in my main function before any tasks etc I had to issue the following
__iar_dynamic_initialization(); // initialize c++ initialized data
I know this isn’t the same for CW but thought it may at least be helpful and may help in were to look
Kevin
Hi Kevin,
I could find or try some of those linker options in CW, specially the "--skip_dynamic_initialization" and only call it after the initialization of MQX and memory. Because i didn't want to change the sources of ARM_GCC_Support provided by CW.
Thank you for the attention.
Best Regards,
Rui Faria
Hello,
I have received an answer from one of our engineers:
Thanks for your feedback! We will consider to provide full C++ support in the future.
Regards,
0xc0170
Hi Martin!
I know that it is not standard, i shouldn't change the sources of ARM_GCC_Support provided by CW but is it only a limitation of Kinetics?
I already use new/malloc in Constructors in Coldfire MCF52259 without problems and now i am porting to Kinetic K70.
Thank you for the attention.
Best Regards,
Rui Faria
I am having a similar problem when I attempt to declare a constant string in a header file as follows:
static const std::string x = "123456789123456789123456789";
I was thinking that the size of this string would be known at compile time and it would be placed in the constants code section. Instead what happens is the static initializer calls the string constructor code. The constructor invokes the memory allocator when the string size is greater than a predetermined value (which this case is). Since mqx was not initialized the program crashes.
I have no problems doing this on other systems, so I believe this is a bug.
Dave
Hi,
I also believe that it is a bug.
The initialization of MQX in Kinectics is wrong.
We have to wait and hope for full C++ support.
Hi I am not sure I agree with your response here is a section copied from the MQXUG.pdf
6.3 Global Constructors
I need to initialize some global constructors, which use the 'new' operator, before I call 'main'; that is, before I start MQX. The 'new' operator calls malloc(), which I redefine to call the MQX function _mem_alloc(). How do I do this?
Initialize the constructors from _bsp_enable_card() (in init_bsp.c), which MQX calls after it initializes the memory management component.
Hello Rui Faria,
we will look into it. I'll share this with the team.
I'll inform you with any changes.
Regards,
0xc0170