[Gtk-sharp-list] widget destruction

Nicholas Frechette zeno490 at gmail.com
Mon Mar 7 15:26:46 EST 2011


If we want to bring up the debate of how gtk resources are destroyed, I
would strongly like to suggest _NOT_ piggy backing on the finalizers to
garbage collect GTK objects.
This will play very poorly with the GC heuristics.
GTK native resources should use the same practices that the rest of the
C#/.net community uses: explicit usage of the dispose pattern.

To highlight why I do not like the current behavior:
1- Create a bunch of widgets
2- Manually remove their references
3- GC kicks in and marks those objects as "dead" and queues them up for the
execution of their finalizers
4- The gtk object finalizers executes, which adds again a reference to that
object so that the GTK main thread can release it
5- The gtk main thread picks up the queued objects for destruction and
destroys them
6- GC runs again and marks those objects as "dead" and queues them up for
the execution of their finalizers
7- The gtk objects finalizers execute and since the is no more work to be
done, the object is finally destroyed and its memory reclaimed

The "clever" use of the finalizer in step 4, in a multi generational GC
heap, will move that object to a longer lived heap and it will take
potentially MUCH longer for step 6 to run on that longer lived heap. You are
effectively promoting soon to be dead objects to a long life object heap
which means their memory will linger in "use" much longer than it would need
to.

It would make much more sense to have the finalizer simply check if the
object has been disposed of manually and emit a warning that the application
is leaking GTK memory instead of circumventing the proper flow of things.
Perhaps that check could only be in debug as well, reducing the overhead in
release/final builds.

2cents since the debate was brought up :)
Nicholas

2011/3/7 "Andrés G. Aragoneses" <knocte at gmail.com>

> This brings up again the debate: should GTK# change the API in the 3.x
> release to make Dispose() methods call Destroy() now that Gtk+3 broke
> API anyway?
>
>
> On 07/03/11 08:15, huseyin cakir wrote:
> > *Hi, we have memory problems in using gtk# in mono 2.8 so we started to
> > use a method that includes dispose-destory of all objects.
> >
> > I mean we use one window widget and change fixed widget in it to use
> > memory effectively and dispose destroy all widgets inside it(for example
> > button widget).
> >
> > but it still increases memory usage in time.
> >
> > _Are there any other usages for less memory usage? Thank you._
> > *
> > *for example:*
> >
> > to navigate between two pages without memory consumption we use a method
> > like below:
> >
> > fixed A fixed B
> > ---------------------------
> > window A
> >
> > add fixed A in window A with other widgets like button widget
> >
> > remove fixed a
> >
> > dispose destroy  fixed A with its children
> >
> > and add fixed B
> >
> >
> *-----------------------------------------------------------------------------------
> > Page 1:*
> >
> > using System;
> > using Gtk;
> >
> > namespace test
> > {
> >      class  Page1 : Fixed
> >      {
> >
> >          Button btn1;
> >          Image img1;
> >          Gdk.Pixbuf px1;
> >
> >
> >          public  Page1()
> >          {
> >
> >              btn1=new Button("go Page2");
> >              px1=new Gdk.Pixbuf("4991.gif");
> >              img1=new Image(px1);
> >              btn1.Clicked+= new EventHandler(btn1_Clicked);
> >              this.Put(btn1,10,10);
> >              this.Put(img1,10,50);
> >
> >
> >          }
> >
> >          void btn1_Clicked(object sender, EventArgs e)
> >          {
> >              freeItems();
> >              Page2 pg2=new Page2();
> >              Test.win.Add(pg2);
> >              Test.win.ShowAll();
> >
> >          }
> >
> >
> >          void freeItems()
> >          {
> >              px1.Dispose();
> >              img1.Dispose();
> >              img1.Destroy();
> >              btn1.Dispose();
> >              btn1.Destroy();
> >              Test.win.Remove(this);
> >              this.Dispose();
> >              this.Destroy();
> >
> >
> >          }
> >
> >      }
> > }
> >
> >
> *-----------------------------------------------------------------------------------
> > Page 2:*
> > using System;
> > using Gtk;
> >
> > namespace test
> > {
> >
> >      class  Page2 : Fixed
> >      {
> >
> >          Button btn1;
> >          Image img1;
> >          Gdk.Pixbuf px1;
> >
> >          public  Page2()
> >          {
> >              btn1=new Button("go back page 1");
> >              px1=new Gdk.Pixbuf("4991.gif");
> >              img1=new Image(px1);
> >
> >              btn1.Clicked+= new EventHandler(btn1_Clicked);
> >
> >              this.Put(btn1,10,10);
> >              this.Put(img1,10,50);
> >
> >          }
> >
> >          void btn1_Clicked(object sender, EventArgs e)
> >          {
> >              freeItems();
> >              Page1 pg1=new Page1();
> >              Test.win.Add(pg1);
> >              Test.win.ShowAll();
> >          }
> >
> >          void freeItems()
> >          {
> >              px1.Dispose();
> >              img1.Dispose();
> >              img1.Destroy();
> >              btn1.Dispose();
> >              btn1.Destroy();
> >              Test.win.Remove(this);
> >              this.Dispose();
> >              this.Destroy();
> >
> >
> >
> > _______________________________________________
> > Gtk-sharp-list maillist  -  Gtk-sharp-list at lists.ximian.com
> > http://lists.ximian.com/mailman/listinfo/gtk-sharp-list
>
>
> _______________________________________________
> Gtk-sharp-list maillist  -  Gtk-sharp-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/gtk-sharp-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/gtk-sharp-list/attachments/20110307/2c8942a6/attachment-0001.html 


More information about the Gtk-sharp-list mailing list