Forums | developer.brewmp.com Forums | developer.brewmp.com

Developer

Forums

Forums:

How can I test if I'm overflowing the stack?

My app runs fine in the emulator, but runs inconsistently on a RAZR Vc3 phone. Execution seems to return from function calls and then jumps off into strange code locations and then hangs.

How can I find how big the stack is on a phone?

and

How can I test if I'm overflowing the stack?

Thanks

Michael O'Brien

Phones in general don't have big stack size. You get around 500 bytes (yes, just bytes not even kilo bytes) of stack.
That means no local array variables in the function, no recursive function calls. Everything should come from heap. Declare all the heavy hitting variables on the heap (the app main stucture).
You can check to make sure you have not, inadvertently, decalred an array in any function. Check for any 'string literals' in the API calls.
Its not going to be an easy task to debug stack issues :-( Sorry, but don't be discouraged !
Do download the 'BREW Debugger 1.1' and use the GDB to debug the app at run time to see what function you were in when the crash happens and dump the local stack variables...
Good luck !

Phones in general don't have big stack size. You get around 500 bytes (yes, just bytes not even kilo bytes) of stack.
That means no local array variables in the function, no recursive function calls. Everything should come from heap. Declare all the heavy hitting variables on the heap (the app main stucture).
You can check to make sure you have not, inadvertently, decalred an array in any function. Check for any 'string literals' in the API calls.
Its not going to be an easy task to debug stack issues :-( Sorry, but don't be discouraged !
Do download the 'BREW Debugger 1.1' and use the GDB to debug the app at run time to see what function you were in when the crash happens and dump the local stack variables...
Good luck !

I thought the debugger required BREW 3.1.3
The RAZR has Brew 3.1.2 so I presumed it can't run the debugger.
The RAZR says it has 17000 bytes stack. I'm well under that number I think.
Michael

I thought the debugger required BREW 3.1.3
The RAZR has Brew 3.1.2 so I presumed it can't run the debugger.
The RAZR says it has 17000 bytes stack. I'm well under that number I think.
Michael

mob wrote:I thought the debugger required BREW 3.1.3
The RAZR has Brew 3.1.2 so I presumed it can't run the debugger.
The RAZR says it has 17000 bytes stack. I'm well under that number I think.
Michael
I really doubt that you are overunning the stack unless you are doing some sort of recursive algorithm. Is there any consitency on where the crash is? As you said, the BREW debugger can not be used until BREW 3.1.3 so you'll have to debug the old fashioned way (DBGPRINTF). Also, do you have any static variables? These work on the emulator, but crash on a device.

mob wrote:I thought the debugger required BREW 3.1.3
The RAZR has Brew 3.1.2 so I presumed it can't run the debugger.
The RAZR says it has 17000 bytes stack. I'm well under that number I think.
Michael
I really doubt that you are overunning the stack unless you are doing some sort of recursive algorithm. Is there any consitency on where the crash is? As you said, the BREW debugger can not be used until BREW 3.1.3 so you'll have to debug the old fashioned way (DBGPRINTF). Also, do you have any static variables? These work on the emulator, but crash on a device.

Thanks for your suggestions.
It was indeed a stack overflow. A deeply buried routine had a structure on the stack that was very large (too large).
One last question: Some recommedations talk about stacks of 500 bytes, yet newer phones like the RAZR have 17K stacks. What is a realistic number today. Is it still 500 ?
I'm trying to find out what API I can use to get the stack size on the phone so I can put in some diagnostic code to measure stack usage. Otherwise I'm running blind just hoping to be under the magic 500 bytes ?
Michael

Thanks for your suggestions.
It was indeed a stack overflow. A deeply buried routine had a structure on the stack that was very large (too large).
One last question: Some recommedations talk about stacks of 500 bytes, yet newer phones like the RAZR have 17K stacks. What is a realistic number today. Is it still 500 ?
I'm trying to find out what API I can use to get the stack size on the phone so I can put in some diagnostic code to measure stack usage. Otherwise I'm running blind just hoping to be under the magic 500 bytes ?
Michael

well, it depends what all devices are you planning to support... if only the new ones of the likes of RAZR then you have some leeway.. but if you want a safety net then I would suggest not putting anything bigger then few bytes on the stack - per session....
I agree with you that it would be nice to have some API to track the stack - just like the Macro for checking heap size...

well, it depends what all devices are you planning to support... if only the new ones of the likes of RAZR then you have some leeway.. but if you want a safety net then I would suggest not putting anything bigger then few bytes on the stack - per session....
I agree with you that it would be nice to have some API to track the stack - just like the Macro for checking heap size...

mob wrote:Thanks for your suggestions.
It was indeed a stack overflow. A deeply buried routine had a structure on the stack that was very large (too large).
One last question: Some recommedations talk about stacks of 500 bytes, yet newer phones like the RAZR have 17K stacks. What is a realistic number today. Is it still 500 ?
I'm trying to find out what API I can use to get the stack size on the phone so I can put in some diagnostic code to measure stack usage. Otherwise I'm running blind just hoping to be under the magic 500 bytes ?
Michael
500 is not really accurate for contemporary devices, but it's a good idea to minimize stack usage as much as possible.
There is no API to determine the free stack space.

mob wrote:Thanks for your suggestions.
It was indeed a stack overflow. A deeply buried routine had a structure on the stack that was very large (too large).
One last question: Some recommedations talk about stacks of 500 bytes, yet newer phones like the RAZR have 17K stacks. What is a realistic number today. Is it still 500 ?
I'm trying to find out what API I can use to get the stack size on the phone so I can put in some diagnostic code to measure stack usage. Otherwise I'm running blind just hoping to be under the magic 500 bytes ?
Michael
500 is not really accurate for contemporary devices, but it's a good idea to minimize stack usage as much as possible.
There is no API to determine the free stack space.

Can we request that an API be put into a future Brew O/S version.
Reliable code can use such an API to detect potential stack overflows and gracefully handle the condition.
Michael O'Brien

Can we request that an API be put into a future Brew O/S version.
Reliable code can use such an API to detect potential stack overflows and gracefully handle the condition.
Michael O'Brien

Write windows emulator code, and then use PENTER functionality of MSVC compiler to do accurate stack measurement.

Write windows emulator code, and then use PENTER functionality of MSVC compiler to do accurate stack measurement.