What I'm trying to do is have a gadget display a number which I store in a TimeType variable as the gadget's data, then on drawing I take the numbers out of the TimeType variable and convert them to a string using StrIToA everything seems to work fine. But calling MemPtrFree does not actually free the memory. Here's the code I'm using minus code that actually does the drawing and manipulating the data.
char *string1; // string pointer def.
string1 = MemPtrNew(sizeof(char) * 7); // allocating string pointer memory
StrIToA(string1, decimal); // decimal is minutes in decimal hours, works fine
// Clean up if (string1) { MemPtrFree(string1); string1 = NULL; }
Other functions I call on the string are StrLen, FntCharsWidth, WinDrawChars.
This code will work, unless somehow your writes to the string are exceeded the length of the buffer and corrupting the memory chunk header. You're allocating seven bytes here... for such a small allocation and one that's bounded in time, you'd be much better off just putting it on the stack by saying
char string[7];
and not freeing anything. The pointer takes up four bytes, so that's only an extra four bytes of stack space.
Now, the StrIToA call is interesting. If you look in StringMgr.h, you'll see this comment and #define:
// Max length of string returned by StrIToA, for -2147483647, plus space // for the terminating null. #define maxStrIToALen 12
That tells me that whenever you call StrIToA, you need a buffer of at least 12 characters. So, change your declaration to
char string[12];
and everything should be good to go.
-- Ben Combee, DTS technical lead, PalmSource, Inc. Read "Combee on Palm OS" at http://palmos.combee.net/
-- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
