473,763 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 );
}
}
}

Nov 15 '05 #1
4 4955
Andreas Huber <ah****@gmx.net > wrote:
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.


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 - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2
Jon Skeet wrote:
Andreas Huber <ah****@gmx.net > wrote:
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.
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).


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:
class Base : IDisposable
{ [snip] // This property is necessary so that subclass methods can check whether // the object has been disposed.
protected bool Disposed
{
get { return this.disposed; }
} [snip] } [snip] class Derived : Base
{
public void SomeOtherMethod ()
{
if ( this.Disposed )
{
throw new ObjectDisposedE xception( "Derived" );
}

// ...
}

[snip]

Regards,

Andreas

Nov 15 '05 #3
Andreas Huber <ah****@gmx.net > wrote:
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).


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


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 - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #4
Jon Skeet wrote:
Andreas Huber <ah****@gmx.net > wrote:
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).


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


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...


I see, sorry for being unclear...

Regards,

Andreas
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6074
by: faktujaa | last post by:
Hi All, A small confusion. I have defined a connection class that has System.Data.IDbConnection as a member variable and implements IDisposable interface. I have implemented Dispose method to call Dispose method of IDbConnection. This Dispose method is called from a destructor. In the dispose method, i also call GC.SuppressFinalize(this) so as to avoid finalizer. All this was OK untill i came across one article that says - not to call...
11
5312
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our 4-month old C#/MC++ .NET project project. I'd like to thank in advance anyone who takes the time to read and/or respond to this message. At a couple points, it may seem like a rant against C# / .NET, but we are pretty firmly stuck with this approach...
4
6731
by: Sunit Joshi | last post by:
Hello All I have an abstract class C1 with this: public abstract class C1 { protected bool m_Dirty; protected override void Dispose(bool disposing) { if(m_Dirty) WriteOuput();
7
4467
by: Tamir Khason | last post by:
I have a class public class Foo { public Foo() { DoSomething() } } I want to be able to do something else while the class enters GC - no more
16
4260
by: Daniel Mori | last post by:
If an object implements the IDisposable interface (regardless if its a framework object or a user object), should I always dispose of that object out of principle?
4
5955
by: Sharon | last post by:
Hi. I put a message box in the form designer, Dispose method. Clicking the form close (X) button, i noticed that the message, appears twice. My code does not call Dispose. What is happening here? Thanks. Sharon.
156
5892
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created inside a method call. dim myvar as new object1 object1.dosomethingmethod(new object2) Note that object 2 has a dispose method but how do I dispose of it unless I do the following:
5
5202
by: Markus Stoeger | last post by:
Hi, I have a class similar to that: class MyClass : IDisposable { IDisposable obj1; IDisposable obj2; IDisposable obj3; MyClass() {
3
4381
by: AlexS | last post by:
When I implement Dispose pattern in object implementing IDisposable, current fxcop recommends: Ensure that Wrapper.Dispose():Void is declared as public and sealed. However, if I do as it asks, compiler complains that sealed can't be used because method is not overrideable. I believe sealed isn't applicable here at all. And this rule shouldn't be public virtual in this case.
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.