Parent document is top of "Motif FAQ (Part 6 of 9)"
Previous document is "206) When can I free data structures passed to or retrieved from"
Next document is "208) Why do I get so many uninitilized memory read (UMR) errors when"

207) What memory leaks are known? Why does my application grow in

size?

[Last modified: July 96]

Answer:  More information about memory leaks (and how you can avoid them) can
be found in two great articles by Kenton Lee:

        http://www.unx.com/DD/advisor/docs/feb96/feb96.klee.shtml and
        http://www.unx.com/DD/advisor/docs/mar96/mar96.klee.shtml

Answer:  Motif 1.0 has many memory leaks, particularly in XmString
manipulation.  Switch to Motif 1.1. 1.2, or 2.0.

Answer:  In Motif 1.2.x, if one is using XmStringGetNextSegment very often
(several 100 times) you'll get very big memory leaks, because one has to
XtFree() also the charset variable and not only the variable holding the text
value.  Thanks to Ingo Schulz (ing@bb-data.de).

Answer:  The Intrinsics have a memory leak in accelerator table management,
and Motif uses this heavily.  Avoid this by mapping/unmapping widgets rather
than creating/destroying them, or get  X11R4 fix-15/16/17.

Answer:  The server may grow in size due to its own memory leaks.  Switch to a
later server.

Answer:  You are responsible for garbage collection in `C'.  Some common cases
where a piece of memory becomes garbage are

 a.  Memory is allocated by Motif for XmStrings by the functions
     XmStringConcat, XmStringCopy, XmStringCreate, XmStringCreateLtoR,
     XmStringCreateLocalized, XmStringDirectionCreate, XmStringNConcat,
     XmStringNCopy, XmStringSegmentCreate, and XmStringSeparatorCreate.  The
     values returned by these functions should be freed using XmStringFree
     when they are no longer needed.

 b.  Memory is allocated by Motif for ordinary character strings (of type
     String) by Motif in XmStringGetLtoR, XmStringGetNextComponent, and
     XmStringGetNextSegment. After using the string, XtFree() it. [Note that
     XmStrings and Strings are two different data types.  XmStrings are
     XmStringFree'd, Strings are XtFree'd.]

 c.  If you have set the label (an XmString) in a label, pushbutton, etc
     widget, free it after calling XtSetValues() or the widget creation
     routine by XmStringFree().

 d.  If you have set text in a text widget, the text widget makes its own
     copy.  Unless you have a use for it, there is no need to keep your own
     copy.

 e.  If you have set the strings in a list widget the list widget makes its
     own copy.  Unless you have a use for it, there is no need to keep your
     own copy.

 f.  When you get the value of a single compound string from a Widget e.g.
     XmNlabelString, XmNmessageString, ... Motif gives you a copy of its
     internal value.  You should XmStringFree this when you have finished with
     it.

 g.  On the other hand, when you get a value of a Table e.g. XmStringTable for
     a List, you get a *pointer* to the internal Table, and should not free
     it.

 h.  When you get the value of the text in a widget by XmTextGetString or from
     the resource XmNvalue, you get a copy of the text.  You should XtFree
     this when you have finished with it.

Answer:  Josef Nelissen wrote:  at least in Motif 1.1.4, X11R4 on a HP 720,
the XmText/XmTextFieldSetString() functions have a memory leak.  The old
value/contents of the Widget isn't freed correctly.  To work around this bug,
one should use a XmText Widget (in single-line-mode) instead of a XmTextField
Widget (the solution fails with XmTextField Widgets !) and replace any

       XmTextSetString(text_widget, str);

by

       XmTextReplace(text_widget, (XmTextPosition) 0,
                     XmTextGetLastPosition(text_widget), str);

Parent document is top of "Motif FAQ (Part 6 of 9)"
Previous document is "206) When can I free data structures passed to or retrieved from"
Next document is "208) Why do I get so many uninitilized memory read (UMR) errors when"