MQX new keyword using C++

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

MQX new keyword using C++

1,600件の閲覧回数
KJFPE
Contributor III

 

Hi, 
Hopefully some can help I am having trouble with the new keyword using C++ below is some example code I am using MQX 3.6.2 and a Kinetis K60.
When the program reaches the line "Test *T1 = new Test;" it halts and generates an MQX_UNHANDLED_INTERRUPT(0x0041)

 

The same is also true for something as simple  as

int *p_scalar = new int(5);

 

 

Many thanks in advance

 

#define START_TASK 5
extern void Start_task(uint_32);
extern "C" const TASK_TEMPLATE_STRUCT  MQX_template_list[] = 
   /* Task Index,   Function,   Stack,  Priority, Name,     Attributes,          Param, Time Slice */
    { START_TASK,   Start_task, 1500,   5,        "hello",  MQX_AUTO_START_TASK, 0,     0 },
    { 0 }
};
class Test
{
public:  
  Test(void);
  ~Test(){};
  
  void Init(void);
 
  int xx;  
};
 
void Start_task(uint_32 initial_data)
{
 printf("Start Task\n"); 
   
   
    Test *T1 = new Test;
    T1->Init();
   
    while(T1->xx)
    {
  _time_delay(200); 
    }
   
  _mqx_exit(0);
}
void Test::Init(void)
{
    printf("Init\n"); 
  xx=1;  
}
Test::Test(void)
{
  printf("Constructor\n");   
  xx=0;
}

 

タグ(3)
1 返信

607件の閲覧回数
unmannedrussery
Contributor II

I struggled with this same problem for a while, and was confused at why the MQX C++ example project worked, but C++ code in my own project did not work. After consulting IAR support, it turns out that the solution is to re-direct the new() and delete() operators via the Linker Options in your project settings. Under Project>Options>Linker>Extra Options, check the "Use command line options" box, and add the folloiwing into the "Command Line Options" text box:

 

--redirect __iar_dlmalloc=malloc
--redirect __iar_dlcalloc=calloc
--redirect __iar_dlfree=free

 

 

This causes IAR redirects new() to __iar_dlmalloc(), and this linker option then redirects __iar_dlmalloc() to malloc(), which is redirected to the MQX _mem_alloc(). This seems like a roundabout way of doing it, but this is actually how Freescale accomplishes it in their C++ example project - take a look at the project settings and you'll find this in there.

 

Hope this helps!