[Date Prev] [Date Next] [Thread Prev] [Thread Next] Date Index Thread Index Search archive:
Date:Mon Nov 26 17:32:16 1993 
Subject:Re: Store leaks (drip drip...) 
From:Jonathan Meyer 
Volume-ID:931126.04 

>(i) The program, I seem to recall, turns over a lot of widgets (several a
>minute). Could that be a problem?

Aaron is correct - the 'C' based X interface is known to have memory leaks in 
it. It is quite informative to run a memory watcher like Purify on a C X 
Toolkit program...

The situation is improving slowly - X11R5 is better than X11R4, and Motif 1.2 
is better than Motif 1.1 (I have no idea about OLIT, but would guess that it 
is at least as bad as Motif 1.1).

Some books on the X Toolkit advise that instead of creating/destroying
lots of widgets, programs should try to reuse widgets wherever possible
to reduce memory leaks.

On a related topic, there is a problem with resource accessing in Xt:

The X Toolkit Intrinsics does not specify whether XtGetValues should return 
the actual data used internally by the widget, or whether it should return a 
copy of the internal data which the caller must free (!!).

This means that accessing the label of a button widget could return either
the internal string or a copy of the string - if it returns a copy the caller
must use XtFree, XmStringFree etc. to free the copy. if it returns the
internal string it would be fatal to call XtFree on it.

Xpt routines currently do not call XtFree on things returned by XtGetValues
(the only safe thing to do), whereas in fact some Motif resources (e.g. the
XmNvalue and XmNlabelString resources) are returned as copies that need to 
be freed...

One workaround is to change application calls so that they explicity use
XtFree themselves, e.g. change

   XptValue(widget, XtN value, TYPESPEC(:XptString)) -> str;

to

   XptValue(widget, XtN value, "exptr") -> str_ptr; ;;; raw external ptr
   exacc :XptString str_ptr -> str; ;;; gets pop string 
   XtFree(str_ptr);                 ;;; frees C string

and similarly for XmStrings:

   XptValue(widget, XtN labelString, "exptr") -> str_ptr; ;;; raw external ptr
   exacc :XpmString str_ptr -> str; ;;; gets pop string 
   XmStringFree(str_ptr);           ;;; frees C XmString 

(this could be wrapped up into convenience routines...)

The same issue arises with XtSetValues.

Jon.