Actually, it's much simpler than that.  It has to do with the call stacks
overwriting each other.  When you call IntToStr(), it places the "char
ptext[7]" on the stack.  Calling StrIToA() fills in the char array, and
hands back a pointer ptext.  You then return ptext.  In the calling
function, it appears that this worked because the data on the stack hasn't
been overwritten yet, since you just returned from IntToStr().  Everything
seems good.

Now, you call SetTextToField().  Entering the function sets up the local
variables on the stack, one of which is occupying the memory location
previously held by ptext, during the execution of IntToStr().  (pform,
pfield, and the one byte of hfield are the likely candidates for which local
vars are occupying the memory previously held by ptext[7].

You now assign to pform, pfield, and hfield, overwriting the memory where
ptext[7] used to live.  Your string is now corrupt.

SO....

What you need to do is declare change IntToStr() to read (I've simplified
it):

CharPtr IntToStr(Word num)
{
 static Char ptext[7];
 return StrIToA(ptext, num);
}

Placing static in front of the declaration of ptext causes it to be
allocated in global space, rather than stack space.  This means the variable
will continue to exist _after_ the call to IntToStr(), and SetTextToField()
won't be able to corrupt it.

Keep in mind that if you do the following, you'll get problems:

CharPtr pOne = IntToStr(1);
CharPtr pTwo = IntToStr(2);

pOne and pTwo will have the same value.  They'll both be pointing at "static
Char ptext[7]" inside IntToStr(), and on inspection of the string, they'll
obviously both say "2".  Caveat emptor.

-David A. Carley
 Independent Palm Programmer



"Nesselhauf, John" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
>
> I believe you must think in terms of where is the variable pointing to
when
> the 2nd function SetTextToField is placed on the stack. It is obviously
not
> pointing at the same location. I am no expert in this but if you approach
it
> from that aspect it may help you determine a correct course of action.
Make
> sure that what is being passed back from IntToStr is a CharPtr. Also, you
> may want to try Locking and Unlocking memory on the variable that is being
> passed.
>
> Hope this helps.
>
> John N.
>
> -----Original Message-----
> From: Matt Becker [mailto:[EMAIL PROTECTED]
> Sent: Friday, November 10, 2000 10:09 AM
> To: Palm Developer Forum
> Subject: An issue of scope???
>
>
> Another question for everyone.  The following line calls the
> SetTextToField function, described below.  Inside this call, it also
> calls IntToStr, as you can see.  Now, these 2 functions are in a
> different file than the call.
> 1st.  The call to IntToStr is passed a number, and it sucessfully passes
> the resultant string back to file1 and sets pnumstr to the string passed
> back.  Now, when SetTextToField is called, the value passed into it is
> random!  Why would pnumstr get miss-passed?  Its like its not passing
> the value into SetTextToField whatsoever.  The value of pnumstr is,
> according to the debugger, correct.  But, when the programs shifts to
> the SetTextToField function, the value of ptext that is passed in is
> garbage.   Is this an issue of scope?  If so, then why doesnt
> MyNum.Number sucessfully get passed into, and back out of IntToStr,
> which is in the same file as SetTextToField.  Please help.  thanks!
>
>
> file1.c
> CharPtr    pnumstr;
> pnumstr = IntToStr(MyNum.Number);
> SetTextToField(ReviewNumField, pnumstr );
>
>
>
> file2.c
> void SetTextToField( Word fieldID, CharPtr ptext )
> {
>  FormPtr  pform;
>  FieldPtr pfield;
>  VoidHand hfield;
>  CharPtr  pfieldtext;
>
>  pform = FrmGetActiveForm();
>  pfield = FrmGetObjectPtr(pform, FrmGetObjectIndex(pform, fieldID));
>  hfield = FldGetTextHandle(pfield);
>  if(hfield != NULL)
>  {
>   MemHandleResize(hfield, StrLen(ptext)+1);
>  }
>  else
>   hfield = MemHandleNew(StrLen(ptext)+1);
>  pfieldtext = MemHandleLock(hfield);
>  StrCopy(pfieldtext, ptext);
>  MemHandleUnlock(hfield);
>  FldSetTextHandle(pfield, hfield);
>  FldDrawField(pfield);
>
>     return;
> }
>
> CharPtr IntToStr(Word num)
> {
>
>  Char  ptext[7];
>  CharPtr  ptemp;
>
>  ptext[0]='\0';
>  ptemp = StrIToA(ptext, num);
>
>  return(ptemp);
> }
>
>
>
>
> --
> For information on using the Palm Developer Forums, or to unsubscribe,
> please see http://www.palmos.com/dev/tech/support/forums/
>
>





-- 
For information on using the ACCESS Developer Forums, or to unsubscribe, please 
see http://www.access-company.com/developers/forums/

Reply via email to