[comp.windows.x.motif] Motif List boxes

jaccard@dssmv2.mpr.ca (Philippe Jaccard) (08/18/90)

I have an application which wishes to dynamically set the items of a list
upon command.
I've used the  XtSetValues function to set the XmNitems and XmNitemCount
property.
As the number of Items is highly variable, I need to allocate/deallocate
memory each time a Initialize the list contents.
I allocate memory to store the array of Xmstring which contents the
items and I call the XtSetValues to store the Items in the List.
I keep a pointer to this array so I can free it before setting a new
one.
It runs great but the size of my executable grows as long as I work with
the list (memory leak).
Does anybody know what does MOTIF do with this array of XmSrring ?
Does the XmList object make an internal copy ?
How do the XmListAddItem and XmListDeleteItem access the list ?
Is XmListDeleteItem the only way to Free the memory space allocated for
the items ?
.
.
.
Any Ideas people?
Philippe

Here follows part of the code(C++) I wrote to access the XmList.

CreateArrayOfString(DoubleLinkedList* list, int &item_count)
{ // CreateArrayOfString

  XmString* string_array;

  // Get the list's size
  int size = list->GetSize();

  // if the list is empty, just return an empty string.
  if(size<=0)
  { // Create an empty string
    string_array = CreateNong[size+1];
  } // Create an empty string
  else
  { // create the array of string

    string_array = new XmString[size+1];

    list->MoveToHead();
    for(int i=0; i<size; i++)
    { // copy all nodes

      // Get current node
      ListNode* node = list->GetCurrentNode();
      string_array[i] = XmStringCreate((char*)node->GetData(),
			(XmStringCharSet)XmSTRING_DEFAULT_CHARSET);

      // Move the current pointer on the following node
      list->MoveToNext();
    } //  copy all nodes
    string_array[size] = NULL;
  } // create the array of string

  // return the item_count
  item_count = size;

  return string_array;
} //CreateArrayOfString

void
SetListItems(Widget    list,
             XmString* new_string,
             int       element_count)
{ // SetListItems

  Arg      arg[3];

  // Replace it with the new list and set the XmNitemCount.
  int i = 0;
  XtSetArg(arg[i], XmNitems, (XtArgVal)(new_string)); i++;
  XtSetArg(arg[i] ,XmNitemCount, (XtArgVal)element_count); i++;
  XtSetValues(list, arg, i);
} // SetListItems
void

FreeArrayOfString(XmString* string_array, int element_count)
{ // FreeArrayOfString

  int i = 0;

  while(i <=  element_count)
  { // free the string
    XmStringFree(string_array[i]);
    i++;
  } // free the string

  // Deallocate the array of string
  delete string_array;
} // FreeArrayOfString

argv@turnpike.Eng.Sun.COM (Dan Heller) (08/18/90)

In article <2304@kiwi.mpr.ca> jaccard@dssmv2.UUCP (Philippe Jaccard) writes:
> As the number of Items is highly variable, I need to allocate/deallocate
> memory each time a Initialize the list contents.

What I do (for the time being) is maintain the list (never deallocate it)
and whenever I need to reset the list, I change the appropriate items
and set-values to the same list.  Basically, I keep a copy of the list
all the time.  The reason this is necessary is hopefully a temporary one.
That is, there is no way to *change* elements in the list -- you must
delete and add items to have the same type of effect (and the visual
feedback of such activity is embarrassing).  Also, you can't get-values
the XmNitems-change an item-reset the list because the internal list
doesn't think it's changed anything so nothing gets redrawn.  On the
other hand,

> Does anybody know what does MOTIF do with this array of XmSrring ?
One of the great mysteries of life.  (Hint: someone reply to this)

> Does the XmList object make an internal copy ?
Yes -- apparently Motif makes good attempts to make internal copies
of most things except for pixmaps.  That is, if you set an icon label
to be a pixmap, don't free it as it is used during the life of the widget
that's using it.

> How do the XmListAddItem and XmListDeleteItem access the list ?
There's nothing magical about that -- it doesn't do anything more
complicated than playing with lists and arrays.

> Is XmListDeleteItem the only way to Free the memory space allocated for
> the items ?
As far as I've seen.
There are a great number of aspects to the List widget that would be
-great- to have access to from the programmer's perspective.  I don't
know why they didn't choose to have public resources available to access
these items.  Internally, they provide everything you'd ever need to
manipulate the list efficiently, but none of these functions or variables
are available outside the package.  I tried to bring this up last time,
but no one answered.  Perhaps someone from OSF would be kind enough to
indicate that one of the following is true:
    1) nothing has changed -- don't expect anything more.
    2) we've added lots of stuff! Life will be much better.
    3) we've made changes, but it's debatable whether you'll be happy with them

--
dan
----------------------------------------------------
O'Reilly && Associates   argv@sun.com / argv@ora.com
Opinions expressed reflect those of the author only.

krader@crg8.sqnt.com (Kurtis D. Rader) (08/20/90)

I've run into the same problem with the List widget never freeing
memory that it allocates.  According to the software engineers here
at Sequent there are a ghastly number of memory leaks in Motif 1.0.
I've also been told that a copy of the strings is made whenever the
list is set.  Deleting each item one at a time (using XmListDeletePos
or a similar call) only slowed down my application; the memory still
leaked away.  I've been promised that version 1.1 has most of the
leaks plugged.
--
-----
Kurtis D. Rader               voice 503/526-3714
Service/Hotline               fax   503/526-3731
Sequent Computer Systems      UUCP  ...{uunet|ogcvax|tektronix}!sequent!krader