From: edmondso@lambda.msfc.nasa.gov (Rich Edmondson)
Subject: please help me with text fields
Organization: NASA/MSFC
Date: Thu, 27 Aug 1992 15:56:39 GMT


I am having difficulty with a display which has been created using
Motif.  Essentially, a form has been created which has 105 label
widgets, 105 text field widgets, and one pushbutton widget placed on
it.  The display calls a work procedure which updates the text fields
with a new string repeatedly (~once a second) until the user presses
the pushbutton.  My problem is that the XmTextFieldSetString call is
malloc-ing 4k of memory every so often (about once through the for
loop below, but not on the same increment each time).  I am a new
Motif programmer and feel like the problem may be something that I
have not configured correctly, but it sure looks like a memory leak in
the Motif toolkit.

These are the attributes for the text field widgets which are set once
during initialization.

	  XtSetArg(text_args[k],XmNx, ((i-1)%3)*180+65); k++;
	  XtSetArg(text_args[k],XmNy, (((i-1)/3)+1)*22); k++;
	  XtSetArg(text_args[k],XmNmarginHeight, 1); k++;
	  XtSetArg(text_args[k],XmNmarginWidth, 1); k++;
	  XtSetArg(text_args[k],XmNeditable,True); k++;
	  XtSetArg(text_args[k],XmNcursorPositionVisible,False); k++;
	  XtSetArg(text_args[k],XmNheight,22); k++;
	  XtSetArg(text_args[k],XmNwidth,100); k++;
	  text_field[i] = XmCreateTextField(form,"text_field[i]",text_args,k);
	  XtManageChild(text_field[i]);

----------------------------------------------------------------------------
This is the majority of the update loop which is defined as the work
procedure in the main loop.  All of the memory that I have malloc-ed
is freed at the bottom.  When I comment out the XmTextFieldSetString
calls, the memory problem goes away.
 
  aos_los = (char **)malloc(3*sizeof(char *));
  for (i=0;i<=2;i++) aos_los[i] = (char *)malloc(20);
  sprintf(aos_los[0],"LOS");
  sprintf(aos_los[1],"AOS");
  sprintf(aos_los[2],"ERROR");


  ECstrm_stat = 2;
  SCstrm_stat = 2;
  Timestrm_stat = 2;

  x = DataFetch(EC_key, 1, &ECstrm_stat);
  x = DataFetch(SC_key, 1, &SCstrm_stat);
  x = DataFetch(Time_key, 1, &Timestrm_stat);

  for (i=0; i<=((2*MAX_PARAMS)+5); i++) 
    {

      if ( i == 0 ) 
	{
	  XmTextFieldSetString(text_field[i],aos_los[Timestrm_stat]);
	}

      if ( i == 1 || i == 2 )
	{
	  XmTextFieldSetString(text_field[i],time_string);
	}

      if ( i == 3 ) 
	{
	  XmTextFieldSetString(text_field[i],aos_los[ECstrm_stat]);
	}

      if ( i >= 4 && i < MAX_PARAMS + 4 )
	{
	  sprintf(float_string,"%f",float_num);
	  XmTextFieldSetString(text_field[i],float_string);
	}

      if ( i == MAX_PARAMS + 5 ) 
	{
	  XmTextFieldSetString(text_field[i],aos_los[SCstrm_stat]);
	}

      if ( i > MAX_PARAMS + 5 )
	{
	  sprintf(float_string,"%f",float_num);
	  XmTextFieldSetString(text_field[i],float_string);
	}
    }

  for (i=0;i<=2;i++) free(aos_los[i]);
  free(aos_los);
  return(False);
}

Is there a function which tells Motif to replace the old string with
the new one?  Is Motif keeping copies of all of the strings which I am
passing?  Help!  I want my memory!

Thanks in advance.

Rich Edmondson		Email to edmondso@lambda.msfc.nasa.gov
NTI
Huntsville, AL