Simple Question

This forum is read only. No new submissions are accepted.

Questions about motif? Contact us

3 posts / 0 new
Last post
Simple Question

Submitted by leetuckert on Fri, 08/10/2001 - 19:32. Developers
Hello

I have a simple question that I probably should know the answer to; however, I don`t. So, here it is

I have the following code

XmString *listItems, *listPtr;
.
. some other code here.
.
/* allocate memory and create strings */
listPtr = listItems = (XmString *) XtCalloc (cnt, sizeof (XmString));
for (x = 0; x < cnt; x++)
{
*listPtr++ = XmStringCreate (data->addr->code,
XmSTRING_DEFAULT_CHARSET);
data = data->next;
}
.
.
. Do something with the strings
.
.
.
/* free the memory */
for (x = 0; x < cnt; x++)
XmStringFree (listItems[x]);

XmStringFree ((XmString) listItems);

Ok. Question Do I need the last XmStringFree? I am freeing each of the elements in the for loop, but do I need to free the pointer itself? The code works either way, but I ran across a piece of code that my associated had written that was causing a problem. She had a static array of XmStrings defined and was making that final call to XmStringFree as above. On some of the interfaces, it was causing odd behavior and core dumps. My array is not static, but dynamic. So, do I need the last XmStringFree or not?

Thanks!

ICS_support

It should be XtFree, not XmStringFree, as it was creating using XtCalloc. The fact that you needed the cast might have been a clue that the type was wrong. The contents of the list are XmStrings, but the outer array is just a generic list of pointers (to which is given the fancy name XmStringTable).

leetuckert

dbl

Thanks for your response. I understand what your are saying. By the way, you seem to answer a lot of questions on this forum. Thanks for taking the time.

Terry (aka tuckert)