I'm using the code found on Palm's Programming Recipes page:
http://www.palmos.com/dev/support/docs/recipes/fields_with_static_text.html

My form has 2 sets of 6 fields, one set are text descriptions and the other
set are their associated values.  Here's how I initialize the fields, again
using the code from the Web site (s_TopSetpoint and s_BottomSetpoint are
indexes into arrays of data for text display):

void MainFieldsFill( FormPtr formP )
{
 FieldType *fldP;
 Char *strBuffer;
 Char i;

 const short *setpointVals = &g_DataBuf.hz;

 strBuffer = (char *) MemPtrNew(20);

 s_BottomSetpoint = s_TopSetpoint;

 for( i = 0; i < NUM_ROWS; i++ )
 {
  // Populate the description fields
  StrCopy( strBuffer, setpointNames[i + s_TopSetpoint] );

  fldP = (FieldType *) FrmGetObjectPtr( formP,
   FrmGetObjectIndex(formP, descFields[i] ) );

  FldFreeMemory( fldP );  // initialize everything, just in case.
  FldSetMaxChars( fldP, StrLen( strBuffer ) );
  FldSetTextPtr( fldP, strBuffer );
  FldRecalculateField( fldP, true );

  // Populate the value fields.
  StrIToA( strBuffer, *(setpointVals + s_TopSetpoint)  );

  fldP = (FieldType *) FrmGetObjectPtr( formP,
   FrmGetObjectIndex( formP, valFields[i] ) );

  FldFreeMemory( fldP );  // initialize everything, just in case.
  FldSetMaxChars( fldP, StrLen( strBuffer ) );
  FldSetTextPtr( fldP, strBuffer );
  FldRecalculateField( fldP, true );

  *setpointVals++;

  if( s_BottomSetpoint == NUM_SETPOINTS )
  {
     s_BottomSetpoint = 0;
  }
  else
  {
     s_BottomSetpoint++;
  }

 } // end of for() loop

} // MainFieldsFill()



And here's how the memory is to be freed in the main form event handler:


case frmCloseEvent:
{
   FieldType *fldP;
     Char i;

     ClosePort();

   frmP = FrmGetActiveForm();

      // Free the buffers we allocated for these fields and gave
      // them as pointers. This is only OK since we know the field
      // is about to be destroyed, and because we know they are
      // pointer-based fields.

   // Description fields.
   for( i = 0; i < NUM_ROWS; i++ )
   {
       fldP = (FieldType *) FrmGetObjectPtr( frmP,
     FrmGetObjectIndex( frmP, descFields[i] ) );

       MemPtrFree((MemPtr) FldGetTextPtr(fldP));
   }

   // Value fields.
   for( i = 0; i < NUM_ROWS; i++ )
   {
       fldP = (FieldType *) FrmGetObjectPtr( frmP,
     FrmGetObjectIndex( frmP, valFields[i] ) );

       MemPtrFree((MemPtr) FldGetTextPtr(fldP));
   }

      break;

} // case frmCloseEvent:


The program crashes after the third iteration through the for loop
indicating an error at MemPtrFree(...) after the loop has, apparently,
successfully freed a couple of description fields.  I verified that the
memory addresses of the fields were being retrieved by FrmGetObjectPtr();
the address of the field that crashed was also correct.

I don't know what's wrong here.  I hope someone else may know.  Thanks.


-Mike





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

Reply via email to