Patrick,
Thanks for posting your code. The problem is indeed highlighted by being able to
see the ChangeTextLab function.
Let's follow what's going on.
StrCopy(ArmedState, "1");
Handle AText = MemHandleNew(8);
StrCopy(MemHandleLock(AText), "Armed");
OK, you've created a handle and copied "Armed" into it.
MakeMyBeep(shortBeep, 3);
ChangeTextLab(MainText164Field, AText);
You've now taken that handle and installed it into a field form object, being
very careful to dispose of the old field object
MemHandleUnlock(AText);
MemHandleFree(AText);
Oops! You just deleted the very same handle that you installed into the field
object. The field object now has a reference to a freed chunk of memory.
Subsequent attempts to manipulate that chunk of memory could easily result in
the crash you see.
I suggest instead that your outer function along the following lines (keeping in
mind that I haven't actually tested this):
if (ArmedState == 0)
{
ArmedState = 1;
Handle AText = MemHandleNew(6);
StrCopy(MemHandleLock(AText), "Armed");
MemHandleUnlock(AText);
MakeMyBeep(shortBeep, 3);
ChangeTextLab(MainText164Field, AText);
}
else
{
ArmedState = 0;
Handle DText = MemHandleNew(9);
StrCopy(MemHandleLock(DText), "Disarmed");
MemHandleUnlock(DText);
MakeMyBeep(shortBeep, 5);
ChangeTextLab(MainText164Field, DText);
}
There are other ways of achieving the same effect, but the above is closest to
what you already have.
As for whether or not a different kind of form object is more suitable, I'll
leave that up to the other thread.
-- Keith
Patrick Ouellet <[EMAIL PROTECTED]> on 07/19/2000 04:50:38 AM
Please respond to "Palm Developer Forum" <[EMAIL PROTECTED]>
Sent by: Patrick Ouellet <[EMAIL PROTECTED]>
To: "Palm Developer Forum" <[EMAIL PROTECTED]>
cc: (Keith Rollin/US/PALM)
Subject: Follow of Invalid Handle
Ok.. I used this to change a Text Field
in my apps...
//this is where I need to change the label
if(StrCompare(ArmedState, "0") == 0)
{
StrCopy(ArmedState, "1");
Handle AText = MemHandleNew(8);
StrCopy(MemHandleLock(AText), "Armed");
MakeMyBeep(shortBeep, 3);
ChangeTextLab(MainText164Field, AText);
MemHandleUnlock(AText);
MemHandleFree(AText);
}
else
{
StrCopy(ArmedState, "0");
Handle DText = MemHandleNew(10);
StrCopy(MemHandleLock(DText), "Disarmed");
MakeMyBeep(shortBeep, 5);
ChangeTextLab(MainText164Field, DText);
MemHandleUnlock(DText); <--Invalid
Handle error
MemHandleFree(DText);
}
MemHandleUnlock(GCode);
//this is the changing label function...
static void ChangeTextLab(Word FieldID, Handle textH)
{
Handle oldTxtH;
FormPtr frm = FrmGetActiveForm();
FieldPtr fldP;
fldP = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, FieldID));
oldTxtH = FldGetTextHandle(fldP);
FldSetTextHandle(fldP, textH);
FldDrawField(fldP);
if(oldTxtH)
MemHandleFree(oldTxtH);
}
But It give me a invalid handle error...
Does someone have a clue....
Or is there a better way to make something like this
simply change the text on a label or non-editable textfield
--
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/