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:

  1. How to print integers, etc into a field
  2. Fix padding issues in structs
  3. Invalid Insertion Point Position via FldSetFocus – what to do
  4. Globals in DAs
  5. FossilIsWristPDADevice causes (Sys 0505) (0,1092) error on non-WristPDA devices

5 Responses to “A very strange StrPrintF crash”

  1. 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.

  2. 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

  3. 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

  4. 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.

  5. Hi Kam,
    no problem!

    Could you maybe contribute some sample code to help us track this down?

    Best regards
    Tam Hanna

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting

© 2013 TamsPalm - the Palm OS / web OS Blog Suffusion theme by Sayontan Sinha