StrPrintF is a derivative of the classic printf – Palm OS programmers who never programmed console applications(like me, I come from the VB side of the river) have little experience with this call. For them, this bit of code looks perfectly ok:
//Prefs.highscores[i].secs is a UInt32
StrPrintF(textparams[6].text,"%2d:%2d %s %s",
prefs.highscores[5].secs%60,
prefs.highscores[5].secs-60*(prefs.highscores[5].secs%60),
prefs.highscores[5].difficulty,
prefs.highscores[5].name);
However, when the app containing this code is run on the Palm OS Simulator, a very strange thing happens. The Palm OS Simulator crashes…on OS level. Windows gives a message about how the Simulator attempted to access memory around 0×000000043 or so(very low memory area) – cool. A Palm OS app that crashes Windows 2000 =).
The problem is the type of the secs field. When you specify a %d, StrPrintF expects a regular int, not a Int32. So, it reads only half of the data into memory, leaving the other half on the stack. When the two %d’s have been processed(one UInt32 taken off the stack, one to go), the %s’ses fetch their pointers off the stack, thereby getting the second UInt32. This then leads to a beautiful crash as experienced above…
This could be handy to keep in mind for another time I use StrPrintF….maybe it can help you all, too!
Related posts:
Funny.
According to the documentation you should use “%ld”, anyhow I want to remember that the “%d” worked just fine in PC programming.
I suppose the palm OS (ups.. Garnet OS … ;-P) implementation is not so robust (although correct).
It seems to be a bug rather hard to debug, nice.
Bye.
Hi Javier,
I also heard of quite a few reports of confusion with endinanness, padding, etc.
The reason why I am posting this stuff here is not to give a specific german programmer reason to call me an idiot(as he often does), but rather to help all of us build a common “library of gotchas” to avoid…
Do you have any to share?
Best regards and thanks for commenting
Tam Hanna
I haven’t read your article fully but
StrPrintF() is a disaster, it’s very poor.
According to the reference it’s meant to return
sorry, my post got messed up, if you allow me to continue:
it prints values incorrectly and sometimes even in the wrong order from the way the format specifier and arg list define.
This on a Treo 680 which is the lastest OS 5 at the time of writing.
Hi Kam,
no problem!
Could you maybe contribute some sample code to help us track this down?
Best regards
Tam Hanna