SV: [Gtk-sharp-list] IDisposable

adam treat manyoso@yahoo.com
Wed, 4 Sep 2002 11:29:15 -0700 (PDT)


Actually we are using GC.SuppressFinalize () but the locking still happens.  I was just writing
some quick descriptive code... Our real code is a bit trickier :-)

--- Steinar Herland <steinar.herland@gecko.no> wrote:
> In short you are missing the call to GC.SuppressFinalize(this);
> 
> Copied from (watch out for line-breaks):
> http://msdn.microsoft.com/library/en-us/cpguide/html/cpconimplementingdi
> sposemethod.asp?frame=true
> 
> 
> [C#]
> // 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()
>    {
>       Dispose(true);
>       // Take yourself off of the Finalization queue 
>       // to prevent finalization code for this object
>       // from executing a second time.
>       GC.SuppressFinalize(this);
>    }
> 
>    // 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(!this.disposed)
>       {
>          // If disposing equals true, dispose all managed 
>          // and unmanaged resources.
>          if(disposing)
>          {
>             // Dispose managed resources.
>             Components.Dispose();
>          }
>          // Release unmanaged resources. If disposing is false, 
>          // only the following code is executed.
>          CloseHandle(handle);
>          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.
>    ~BaseResource()      
>    {
>       // Do not re-create Dispose clean-up code here.
>       // Calling Dispose(false)is optimal in terms of
>       // readability and maintainability.
>       Dispose(false);
>    }
> 
>    // 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()
>    {
>       if(this.disposed)
>       {
>          throw new ObjectDisposedException();
>       }
>    }
> }
> 
> -----Opprinnelig melding-----
> Fra: adam treat [mailto:manyoso@yahoo.com] 
> Sendt: 4. september 2002 19:51
> Til: Miguel de Icaza
> Kopi: Mike Kestner; gtk-sharp-list@ximian.com
> 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
> threads.
> 
> --- Miguel de Icaza <miguel@ximian.com> 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  -  Gtk-sharp-list@ximian.com
> > http://lists.ximian.com/mailman/listinfo/gtk-sharp-list
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Finance - Get real-time stock quotes
> http://finance.yahoo.com
> 
> _______________________________________________
> Gtk-sharp-list maillist  -  Gtk-sharp-list@ximian.com
> http://lists.ximian.com/mailman/listinfo/gtk-sharp-list
> 
> _______________________________________________
> Gtk-sharp-list maillist  -  Gtk-sharp-list@ximian.com
> http://lists.ximian.com/mailman/listinfo/gtk-sharp-list


__________________________________________________
Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
http://finance.yahoo.com