SV: [Gtk-sharp-list] IDisposable

Steinar Herland
Wed, 4 Sep 2002 20:14:11 +0200

In short you are missing the call to GC.SuppressFinalize(this);

Copied from (watch out for line-breaks):

// Design pattern for the base class.
// By implementing IDisposable, you are announcing that instances
// of this type allocate scarce resources.
public class BaseResource: IDisposable
   // Pointer to an external unmanaged resource.
   private IntPtr handle;
   // Other managed resource this class uses.
   private Component Components;
   // Track whether Dispose has been called.
   private bool disposed = false;

   // Constructor for the BaseResource Object.
   public BaseResource()
      // Insert appropriate constructor code here.

   // Implement Idisposable.
   // Do not make this method virtual.
   // A derived class should not be able to override this method.
   public void Dispose()
      // Take yourself off of the Finalization queue 
      // to prevent finalization code for this object
      // from executing a second time.

   // Dispose(bool disposing) executes in two distinct scenarios.
   // If disposing equals true, the method has been called directly
   // or indirectly by a user's code. Managed and unmanaged resources
   // can be disposed.
   // If disposing equals false, the method has been called by the 
   // runtime from inside the finalizer and you should not reference 
   // other objects. Only unmanaged resources can be disposed.
   protected virtual void Dispose(bool disposing)
      // Check to see if Dispose has already been called.
         // If disposing equals true, dispose all managed 
         // and unmanaged resources.
            // Dispose managed resources.
         // Release unmanaged resources. If disposing is false, 
         // only the following code is executed.
         handle = IntPtr.Zero;
         // Note that this is not thread safe.
         // Another thread could start disposing the object
         // after the managed resources are disposed,
         // but before the disposed flag is set to true.
      disposed = true;         

   // Use C# destructor syntax for finalization code.
   // This destructor will run only if the Dispose method 
   // does not get called.
   // It gives your base class the opportunity to finalize.
   // Do not provide destructors in types derived from this class.
      // Do not re-create Dispose clean-up code here.
      // Calling Dispose(false)is optimal in terms of
      // readability and maintainability.

   // Allow your Dispose method to be called multiple times,
   // but throw an exception if the object has been disposed.
   // Whenever you do something with this class, 
   // check to see if it has been disposed.
   public void DoSomething()
         throw new ObjectDisposedException();

-----Opprinnelig melding-----
Fra: adam treat [] 
Sendt: 4. september 2002 19:51
Til: Miguel de Icaza
Kopi: Mike Kestner;
Emne: Re: [Gtk-sharp-list] IDisposable

Ok, we tried to have the Dispose method unregister the objects in the
hash, but there was a
problem with Monitor locking and the GC:

  ~Pixbuf ()
    Dispose ();

  Dispose ()
    UnregisterObject ();

This should work, but when the program thread tries to access the hash
for introspection and the
GC also tries to access the hash, the program freezes, regardless
whether it is lock()ed in both

--- Miguel de Icaza <> wrote:
> > Right, but Dispose will never be called if the object is in the hash
> Dispose is called by users of IDisposable, manually.
> So I do things like:
> 	Pixbuf p = new Pixbuf ("a.png");
> 	p.RenderToDrawable (...);
> 	p.Dispose ();
> You are thinking about ~Pixbuf () which is indeed never called if you
> are not stored in a WeakReference
> Miguel
> _______________________________________________
> Gtk-sharp-list maillist  -

Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes

Gtk-sharp-list maillist  -