Your function below, IntToStr, is flawed.  Look at it again:

CharPtr IntToStr(Word num)
{

 Char  ptext[7];
 CharPtr  ptemp;

 ptext[0]='\0';
 ptemp = StrIToA(ptext, num);

 return(ptemp);
}

You are returning a pointer to a char array allocated on the stack.  Thus,
when the return statement is "executed," ptemp (the resultant returned
pointer) is pointing to deallocated memory (and is now garbage).  Sometimes
this will work, and sometimes this will not.  This is what I call "running
by luck."  IntToStr should be re-written to something like this:

CharPtr IntToStr(Word num)
{
 CharPtr  ptemp;
 ptemp = MemPtrAlloc(16);

 return (StrIToA(ptemp, num));
}

The caller of this routine will be responsible for eventually calling
MemPtrFree on the returned pointer.

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of
Nesselhauf, John
Sent: Friday, November 10, 2000 12:59 PM
To: Palm Developer Forum
Subject: RE: An issue of scope???


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 Palm Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/tech/support/forums/


-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to