Dispose pattern improvement?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andreas Huber

    #1

    Dispose pattern improvement?

    Hi there

    Is there a reason why .NET framework base classes implementing IDisposable
    (like e.g. System.Componen tModel.Componen t) do not prevent multiple calls to
    the protected virtual void Dispose( bool disposing ) function?

    Derived classes would then no longer need to check whether they have been
    disposed already and the associated bool member wouldn't be necessary
    either.

    Example code:

    class Base : IDisposable
    {
    private bool disposed = false;

    ~Base()
    {
    if ( !this.disposed ) // *** here ***
    {
    Dispose( false );
    this.disposed = true;
    }
    }

    public void Dispose()
    {
    if ( !this.disposed ) // *** here ***
    {
    Dispose( true );
    GC.SuppressFina lize( this );
    this.disposed = true;
    }
    }

    public void SomeMethod()
    {
    if ( this.Disposed )
    {
    throw new ObjectDisposedE xception( "Base" );
    }

    // ...
    }

    // This property is necessary so that subclass methods can check whether
    // the object has been disposed.
    protected bool Disposed
    {
    get { return this.disposed; }
    }

    protected virtual void Dispose( bool disposing )
    {
    if ( disposing )
    {
    // release my managed resources
    }

    // release my unmanaged resources
    }
    }


    class Derived : Base
    {
    public void SomeOtherMethod ()
    {
    if ( this.Disposed )
    {
    throw new ObjectDisposedE xception( "Derived" );
    }

    // ...
    }

    protected override void Dispose( bool disposing )
    {
    try
    {
    if ( disposing )
    {
    // release my managed resources
    }

    // release my unmanaged resources
    }
    finally
    {
    base.Dispose( disposing );
    }
    }
    }

  • Jon Skeet

    #2
    Re: Dispose pattern improvement?

    Andreas Huber <ah2003@gmx.net > wrote:[color=blue]
    > Is there a reason why .NET framework base classes implementing IDisposable
    > (like e.g. System.Componen tModel.Componen t) do not prevent multiple calls to
    > the protected virtual void Dispose( bool disposing ) function?
    >
    > Derived classes would then no longer need to check whether they have been
    > disposed already and the associated bool member wouldn't be necessary
    > either.[/color]

    In what way would the associated bool member not be necessary? You'd
    still need to check whether or not the object had been disposed for
    methods *other* than dispose itself (eg writing to a stream which has
    already been disposed).

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Andreas Huber

      #3
      Re: Dispose pattern improvement?

      Jon Skeet wrote:[color=blue]
      > Andreas Huber <ah2003@gmx.net > wrote:[color=green]
      >> Is there a reason why .NET framework base classes implementing
      >> IDisposable (like e.g. System.Componen tModel.Componen t) do not
      >> prevent multiple calls to the protected virtual void Dispose( bool
      >> disposing ) function?
      >>
      >> Derived classes would then no longer need to check whether they have
      >> been disposed already and the associated bool member wouldn't be
      >> necessary either.[/color]
      >
      > In what way would the associated bool member not be necessary? You'd
      > still need to check whether or not the object had been disposed for
      > methods *other* than dispose itself (eg writing to a stream which has
      > already been disposed).[/color]

      I'm not sure I understand what you mean as that's what the protected
      Disposed property in the base class would be for...

      I quote from my original message:
      [color=blue]
      > class Base : IDisposable
      > {[/color]
      [snip][color=blue]
      > // This property is necessary so that subclass methods can check[/color]
      whether[color=blue]
      > // the object has been disposed.
      > protected bool Disposed
      > {
      > get { return this.disposed; }
      > }[/color]
      [snip][color=blue]
      > }[/color]
      [snip][color=blue]
      > class Derived : Base
      > {
      > public void SomeOtherMethod ()
      > {
      > if ( this.Disposed )
      > {
      > throw new ObjectDisposedE xception( "Derived" );
      > }
      >
      > // ...
      > }[/color]
      [snip]

      Regards,

      Andreas

      Comment

      • Jon Skeet

        #4
        Re: Dispose pattern improvement?

        Andreas Huber <ah2003@gmx.net > wrote:[color=blue][color=green]
        > > In what way would the associated bool member not be necessary? You'd
        > > still need to check whether or not the object had been disposed for
        > > methods *other* than dispose itself (eg writing to a stream which has
        > > already been disposed).[/color]
        >
        > I'm not sure I understand what you mean as that's what the protected
        > Disposed property in the base class would be for...[/color]

        Ah - sorry, I thought you were suggesting that the bool member in your
        example wouldn't be necessary. I wasn't entirely clear about what you
        were suggesting...

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Andreas Huber

          #5
          Re: Dispose pattern improvement?

          Jon Skeet wrote:[color=blue]
          > Andreas Huber <ah2003@gmx.net > wrote:[color=green][color=darkred]
          >>> In what way would the associated bool member not be necessary? You'd
          >>> still need to check whether or not the object had been disposed for
          >>> methods *other* than dispose itself (eg writing to a stream which
          >>> has already been disposed).[/color]
          >>
          >> I'm not sure I understand what you mean as that's what the protected
          >> Disposed property in the base class would be for...[/color]
          >
          > Ah - sorry, I thought you were suggesting that the bool member in your
          > example wouldn't be necessary. I wasn't entirely clear about what you
          > were suggesting...[/color]

          I see, sorry for being unclear...

          Regards,

          Andreas

          Comment

          Working...