CtlSetLabel just redirects the pointer to the control's text to 
another pointer you provide. As a result, the pointer needs to be 
valid whenever the control is drawn or updated.

See notes below:

>    else if (eventP->data.ctlEnter.controlID == MainExecButton)
>      {
>       if (FrmValidatePtr(frmP)) {
>    /* the four lines below are the cause of the problems */
>
>           x  = MemPtrNew(100);

1. You've allocated a new chunk and assigned 'x' to point to the 
chunk. This might be a start toward what you want, but you would want 
'x' to be a global or static and set the size of the chunk to 
FldGetTextLength(pFld) + 1.

>        x = FldGetTextPtr(FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP,
>MainTextField)));

2. You've changed 'x' to point to the text field text. The chunk is 
now orphaned, creating a memory leak, since you no longer have a 
reference to it.

>        CtlSetLabel(FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP,
>MainRespLabel)), x);

3. This sets the control's label field to point to the text in the 
text field. I suspect this will lead to problems if the user tries to 
edit the field text, but I'd have to try it to see. At any rate, this 
seems dangerous to me.

>        MemPtrFree(x);

4. Boom. You are trying to deallocate the text field's pointer, which 
is a Very Bad Thing (and unintended).

>          }
>      handled = true;
>      }
>    break;
>
>   default:
>    break;
>
>   }
>  return handled;
}

My recommendation: do a search through the Palm OS Examples for the 
CtlSetLabel function and see how the Palm engineers use it in the ROM 
applications.

JB

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