Re: custom tree model, iters, and memory management



Hi,

On Sun, Jul 26, 2009 at 8:22 PM, Thomas Stover<thomas wsinnovations com> wrote:
While working on a custom tree model (thanks to Tim-Philipp Müller for the
tutorial), I needed more pointers in my iter structure than the user_data,
user_data2, and user_data3. So I defined a new structure and stored that in
user_data. For the model's _get_iter(), _iter_children(), _iter_parent(),
iter_nth_child() methods, if the "output" iter's user_data is NULL, a new
one of these "extra pointers structures" gets allocated. At the moment, all
of these allocations get also referenced in a GSList in the custom model,
and freed when the model is finalized. While this does work ok, it feels
strange. Any thoughts? Some light analysis shows the treeview doesn't use
that many iters, so maybe this is fine?

Feels strange indeed :)  One of the problems is that you do not really
know when an iterator is no longer used.  This also depends on whether
your model hands out persistent or non-persistent iterators.
Persistent iterators stay valid for ever, non-persistent stay valid
until the model changes its stamp, which is whenever the internal data
structure of the model changes in such a way that the iterator for the
same node will be different.  For the persistent case, you need to
keep all these extra allocated structures around until finalization,
if you really allocate a new structure for every iterator handed out,
the memory usage here is not bounded.

I would suggest you to have a look at how GtkTreeModelSort and
GtkTreeModelFilter handle this.  A valid iterator only exists for a
row in your model.  You can group all the data you need for each row
in a structure and group these structures in a list or tree form
representing the layout of your model.  Each row now has a structure
with all required data and is accessible by a pointer.  Instead of
allocating a new structure for each iterator, put the pointer to the
(already allocated) structure in the model's internal data structure
in the iterator's user_data pointer.  No need to allocate a new
structure each time and the other model methods of your model can use
the pointer in the iterator's user_data to access the internal data
structure.

Hope this helps and clear things up a bit.


regards,

-kris.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]