Ticket Created
over 3 years ago

WERETECH-12291

Better stackoverflow debugging

E.g. following does lead to a stack overflow exception:

  • endless recursion - of course, this is the obvious reason
  • to many lines of code and/or functions - seems like functions are put on the stack and at some point the stack overflows

The second porblem is not that obvious and as far as I have found out until now, there is no debugging utility to show how near we get to this limit. Could this be added in the future? Not knowing about this limitation is very dangerous but even if we know it we don't know how near we are to this limit and some code paths may add more functions to the stack than others...

Background

I have following logic in my watchface:

  • in every onUpdate I iterate over all displayed data fields
  • then I calculate data for each data field like icon, text and so on - this is done in a single "calcData(dataType, ...)" function
  • then I render the calculated data

I ran into a stackoverflow error after adding some new features inside calcData although there was no recursion at all. Took me some time to find the culprit - in the end I simply split up my "calcData(...)" into a few smaller sub functions and everything was working again without errors. (funny side note, I made the single calc function in the past because every function adds size to the prg and I needed to save some size and tried to optimise every single bit back than for one of my watchfaces as I was very close to the size limits on 92kB devices)

Problem

I had no idea where to start and e.g. commenting out some parts of the code did not help, deleting the same part of code did help => weird observation, but seems like the comments are part of the function on the stack as well... Nevertheless, having some debugging tool for this sort of problem like seeing how much of the stack is used would help a lot.

In my example, memory and peak memory, objects and everything I could find in the SIM debugger was far away from their limits and still I faced the stack overflow exception...