473,847 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Implementing IDisposable on a Stream subclass

Hi,

I have a custom subclass of System.IO.Strea m type. I
wonder how to correctly implement the IDisposable pattern
in this situation.

The parent Stream type apparently uses explicit interface
implementation, and I could not find a way for my child
type to override parent's IDisposable.Dis pose() method.
The intention was to clean up child's resources first,
then call parent's Dispose() method.

What first comes to mind is to inherit the IDisposable
interface directly in my child type, despite the parent
already does so, and implement my own Dispose pattern like
Jeff Ricther recommends in his book.

On the other hand, the Framework types like FileStream
seem to somehow override their parent Stream's Dispose()
method...

Any ideas?

Thanks,

Ivan.
Nov 15 '05 #1
3 4509
The Close method is virtual. The base class will call Close when it's
disposed or finalized.

The bottom line is that I think all you need to do is implement a Close
method like so:

public override Close()
{
base.Close();
if(!AlreadyClos ed) {
// do my own cleanup
AlreadyClosed = true;
}
}

"Ivan Neganov" <an*******@disc ussions.microso ft.com> wrote in message
news:2b******** *************** *****@phx.gbl.. .
Hi,

I have a custom subclass of System.IO.Strea m type. I
wonder how to correctly implement the IDisposable pattern
in this situation.

The parent Stream type apparently uses explicit interface
implementation, and I could not find a way for my child
type to override parent's IDisposable.Dis pose() method.
The intention was to clean up child's resources first,
then call parent's Dispose() method.

What first comes to mind is to inherit the IDisposable
interface directly in my child type, despite the parent
already does so, and implement my own Dispose pattern like
Jeff Ricther recommends in his book.

On the other hand, the Framework types like FileStream
seem to somehow override their parent Stream's Dispose()
method...

Any ideas?

Thanks,

Ivan.

Nov 15 '05 #2
Add the GC.SuppressFina lize to the close method to avoid being called twice
to clean up resources.

A Dispose method should call the GC.SuppressFina lize method for the object
it is disposing. If the object is currently on the finalization queue,
GC.SuppressFina lize prevents its Finalize method from being called.
Remember that executing a Finalize method is costly to performance. If your
Dispose method has already done the work to clean up the object, then it is
not necessary for the garbage collector to call the object's Finalize
method.
public override Close()
{
base.Close();
if(!AlreadyClos ed) {
// do my own cleanup
AlreadyClosed = true;
}
GC.SuppressFina lize(this);
}
--------------------
From: "Ted Miller" <te*@nwlink.com >
Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
Subject: Re: Implementing IDisposable on a Stream subclass
Date: Tue, 11 Nov 2003 10:17:44 -0800
Organization : Posted via Supernews, http://www.supernews.com
Message-ID: <vr************ @corp.supernews .com>
References: <2b************ *************** *@phx.gbl>
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
X-Complaints-To: ab***@supernews .com
Lines: 45
Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!new sfeed00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.s yr.edu!news.max well.syr.edu!sn-xit-03!sn-xit-04!sn-
xit-01!sn-post-01!supernews.co m!corp.supernew s.com!not-for-mailXref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1984 49
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp

The Close method is virtual. The base class will call Close when it's
disposed or finalized.

The bottom line is that I think all you need to do is implement a Close
method like so:

public override Close()
{
base.Close();
if(!AlreadyClos ed) {
// do my own cleanup
AlreadyClosed = true;
}
}

"Ivan Neganov" <an*******@disc ussions.microso ft.com> wrote in message
news:2b******* *************** ******@phx.gbl. ..
Hi,

I have a custom subclass of System.IO.Strea m type. I
wonder how to correctly implement the IDisposable pattern
in this situation.

The parent Stream type apparently uses explicit interface
implementation, and I could not find a way for my child
type to override parent's IDisposable.Dis pose() method.
The intention was to clean up child's resources first,
then call parent's Dispose() method.

What first comes to mind is to inherit the IDisposable
interface directly in my child type, despite the parent
already does so, and implement my own Dispose pattern like
Jeff Ricther recommends in his book.

On the other hand, the Framework types like FileStream
seem to somehow override their parent Stream's Dispose()
method...

Any ideas?

Thanks,

Ivan.


Rakesh, EFT.

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Nov 15 '05 #3
Thanks a lot!
-----Original Message-----
Add the GC.SuppressFina lize to the close method to avoid being called twiceto clean up resources.

A Dispose method should call the GC.SuppressFina lize method for the objectit is disposing. If the object is currently on the finalization queue,GC.SuppressFin alize prevents its Finalize method from being called.Remember that executing a Finalize method is costly to performance. If yourDispose method has already done the work to clean up the object, then it isnot necessary for the garbage collector to call the object's Finalizemethod.
public override Close()
{
base.Close();
if(!AlreadyClos ed) {
// do my own cleanup
AlreadyClosed = true;
}
GC.SuppressFina lize(this);
}
--------------------
From: "Ted Miller" <te*@nwlink.com >
Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
Subject: Re: Implementing IDisposable on a Stream subclassDate: Tue, 11 Nov 2003 10:17:44 -0800
Organizatio n: Posted via Supernews, http://www.supernews.comMessage-ID: <vr************ @corp.supernews .com>
References: <2b************ *************** *@phx.gbl>
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
X-Complaints-To: ab***@supernews .com
Lines: 45
Path:cpmsftngxa06.p hx.gbl!TK2MSFTN GP08.phx.gbl!

newsfeed00.sul. t-online.de!t-online.de!news-spur1.maxwell.s yr.edu!news.max well.syr.edu!sn- xit-03!sn-xit-04!sn-xit-01!sn-post-01!supernews.co m!corp.supernew s.com!not- for-mail
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1984 49X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp

The Close method is virtual. The base class will call Close when it'sdisposed or finalized.

The bottom line is that I think all you need to do is implement a Closemethod like so:

public override Close()
{
base.Close();
if(!AlreadyClos ed) {
// do my own cleanup
AlreadyClosed = true;
}
}

"Ivan Neganov" <an*******@disc ussions.microso ft.com> wrote in messagenews:2b****** *************** *******@phx.gbl ...
Hi,

I have a custom subclass of System.IO.Strea m type. I
wonder how to correctly implement the IDisposable pattern in this situation.

The parent Stream type apparently uses explicit interface implementation, and I could not find a way for my child
type to override parent's IDisposable.Dis pose() method.
The intention was to clean up child's resources first,
then call parent's Dispose() method.

What first comes to mind is to inherit the IDisposable
interface directly in my child type, despite the parent
already does so, and implement my own Dispose pattern like Jeff Ricther recommends in his book.

On the other hand, the Framework types like FileStream
seem to somehow override their parent Stream's Dispose () method...

Any ideas?

Thanks,

Ivan.


Rakesh, EFT.

This posting is provided "AS IS" with no warranties, and

confers no rights.Use of included script samples are subject to the terms specified athttp://www.microsoft.com/info/cpyright.htm

.

Nov 15 '05 #4

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

Similar topics

7
3021
by: Willem van Rumpt | last post by:
Hi all, coming from an unmanaged programming background, I took my time to sort out the IDisposable and finalizer patterns. Just when I thought I had it all conceptually neatly arranged, the "Close()" methods reared their ugly (at least it would seem...)heads. I was happily delving away in the .NET framework, investigating the stream classes with the msdn and Lutz Roeder's .NET reflector, when I stumbled upon the following:
7
2100
by: Scott M. | last post by:
In a typical class, do I need to indicate that it implements the IDisposable interface and then create a Dispose method that implements the Dispose required by the IDisposable interface or can I just make a Sub Dispose() and the CLR will know that this is the Dispose method to call when the object falls out of scope? Thanks.
6
1300
by: Narshe | last post by:
If I implement IDisposable in a custom class, how do I let the GC know that the custom class objects aren't being used anymore, and are ready for cleanup? Do I just set them equal to null? Do I need to call GC.Collect() after? Is IDisposable only meant for objects that already implement it like sockets and stream? Thanks.
11
1738
by: David Thielen | last post by:
Hi; I am writing a class that implements IDbConnection. The i/f defines a method BeginTransaction() that returns an IDbTransaction. I want to define this as returning a DbTransaction (as DbConnection does). It makes sense to me that this is legal as DbTransaction implements IDbTransaction so it does meet the contract of returning an IDbTransaction. But it won't compile - how do I get this to work? There must be a way as DbConnection...
4
2037
by: Helge Jensen | last post by:
In C# 2.0 System.IO.Stream is declared as: public class Stream: ..., IDisposable { ... public void Dispose(); public void Dispose(bool); IDisposable.Dispose(); } Which must be a design-blunder, if not a 100-year sleep. It prevents
4
3337
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue, how are you sure that your managed object resources are completely freed up of resources it's might be using? In my case below I have a private bool variable. Are there any other managed resource that you might need to explicitly free up in
12
7993
by: Mark Rae | last post by:
Hi, The following code works: HttpWebRequest objRequest = null; try { HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com"); using (HttpWebResponse objResponse =
0
9727
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10978
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10330
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
9477
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...
1
7879
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7050
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
5716
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...
0
5907
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4113
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.