C++ App works fine on emulator but crashes on devic | developer.brewmp.com C++ App works fine on emulator but crashes on devic | developer.brewmp.com

Developer

C++ App works fine on emulator but crashes on devic

Forums:

Hi,

We are working on a BREW application which is created using Brew SDK 3.1.4 and is entirely in C++. It works perfectly in simulator but when we try to download it on a device in DeviceAnywhere, the device crashes - it basically restarts ( device is test enabled as we tried running a basic HelloWorld app on it which worked fine)

To find the root cause of the problem, we enhanced the basic Hello world application to use memory allocation using new operator ( overridden with malloc - as suggested on a number of posts in this forum) and tried running it on the device. Again the device restarted.

We also tried creating a log file with debug prints to check the flow of the application and where exactly it crashes but the log file never gets created on DA.

The code we are trying to run is given below ( also attached are the required files- helloworld.cpp, test.cpp. test.h). Please have a look for what is wrong with this code and why it always makes the device restarts.

Code: trying to display Hello Test
static boolean HelloWorld_HandleEvent(AEEApplet * pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{
test * obj;
obj = new test();
obj->i = 20;
obj->b = true;
obj->a1 = 'T';
obj->a2 = 'E';
obj->a3 = 'S';
obj->a4 = 'T';

AECHAR szText[] = {'H','e','l','l','o',' ',obj->a1,obj->a2,obj->a3,obj->a4,'\0'};

switch (eCode){
case EVT_APP_START:
IDISPLAY_DrawText(pMe->m_pIDisplay, // Display instance
AEE_FONT_BOLD, // Use BOLD font
szText, // Text - Normally comes from resource
-1, // -1 = Use full string length
0, // Ignored - IDF_ALIGN_CENTER
0, // Ignored - IDF_ALIGN_MIDDLE
NULL, // No clipping
IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
IDISPLAY_Update (pMe->m_pIDisplay);

return(TRUE);

case EVT_APP_STOP:
return(TRUE);

default:
break;
}
return(FALSE);

//AECHAR szText[] = {'H','e','l','l','o',' ','W','o', 'r', 'l', 'd','\0'};
test * obj;
obj = new test();
obj->i = 20;
obj->b = true;
obj->a1 = 'T';
obj->a2 = 'E';
obj->a3 = 'S';
obj->a4 = 'T';
AECHAR szText[] = {'H','e','l','l','o',' ',obj->a1,obj->a2,obj->a3,obj->a4,'\0'};
can you please move the above code from event handle function to initapp or any other function and try ?
your app handle event receives EVT_APP_START_WINDOW before receiving EVT_APP_START, so you will allow creating test object twice without bothering to release and this helps to memory leak.
please debug your application by putting some dbgprintf on device.

//AECHAR szText[] = {'H','e','l','l','o',' ','W','o', 'r', 'l', 'd','\0'};
test * obj;
obj = new test();
obj->i = 20;
obj->b = true;
obj->a1 = 'T';
obj->a2 = 'E';
obj->a3 = 'S';
obj->a4 = 'T';
AECHAR szText[] = {'H','e','l','l','o',' ',obj->a1,obj->a2,obj->a3,obj->a4,'\0'};
can you please move the above code from event handle function to initapp or any other function and try ?
your app handle event receives EVT_APP_START_WINDOW before receiving EVT_APP_START, so you will allow creating test object twice without bothering to release and this helps to memory leak.
please debug your application by putting some dbgprintf on device.

Hi Manju,
Thanks for the reply. Surprisingly it really worked!!
But I didn't understand the logic clearly. If I know the correct flow, as soon as app is launched, AEECreateInstance() -> InitAppData() -> EVT_APP_START case is the flow and in simulator if I create my object instance, it doesnt create any problem.
So can you/anyone else explain me the reason for not this working if I create instance under EVT_APP_START? Does this mean that I should not create any object instantiation (doing using new by overriding MALLOC) within any case under handleEvent()? e.g. under EVT_APP_KEY at runtime I may want to create another instance, it wont work?
I have so much confusion on this. It would be great to get the clarification from somebody.
Regards,
Aahna

Hi Manju,
Thanks for the reply. Surprisingly it really worked!!
But I didn't understand the logic clearly. If I know the correct flow, as soon as app is launched, AEECreateInstance() -> InitAppData() -> EVT_APP_START case is the flow and in simulator if I create my object instance, it doesnt create any problem.
So can you/anyone else explain me the reason for not this working if I create instance under EVT_APP_START? Does this mean that I should not create any object instantiation (doing using new by overriding MALLOC) within any case under handleEvent()? e.g. under EVT_APP_KEY at runtime I may want to create another instance, it wont work?
I have so much confusion on this. It would be great to get the clarification from somebody.
Regards,
Aahna