[OLPC-devel] Re: [sugar] Pygtk and garbage collecting
Federico Mena Quintero
federico at ximian.com
Wed Aug 2 12:41:32 EDT 2006
On Mon, 2006-07-31 at 17:32 +0200, Philip Van Hoof wrote:
> In case of tinymail the GtkTreeModel that holds a reference on all the
> headers of the current folder, is such an object.
>
> model = MsgHeaderListModel ()
> treeview.set_model (model)
>
> newmodel = MsgHeaderListModel ()
> treeview.set_model (newmodel)
>
> Will not immediately mark "model" for garbage collection (when model
> goes out of scope). Adding a gc.collect(), however, will.
The usual idiom is to have a way to explicitly dispose an object which
holds a reference to a non-GC resource. In C# you do
obj = new SomeObject ();
treeview.set_model (obj);
newobj = new SomeObject ();
treeview.set_model (newobj);
obj.Dispose ();
or use syntactic sugar:
using (Gdk.Pixbuf pixbuf = new Gdk.Pixbuf ("foo.jpg")) {
do_something_with_pixbuf (pixbuf);
} /* exiting the "using" block disposes the pixbuf */
This is pretty useful for managed objects which maintain non-managed
data (file descriptors, memory buffers, database connections, etc.).
Does Python let you do something like this?
Federico
More information about the Devel
mailing list