473,325 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Is Dispose called on parent classes?


If I have two classes both of which have unmanaged resources that need to be cleaned up. For example class B is a child of class A
and I create an instance of class B. Then, if I call B.Dispose - Does A.Dispose get called automatically or do I have to clean up
the unmanaged resource in class A as well in B.Dispose?

Thanks,

Steve
Nov 17 '05 #1
3 1474
Steve,

No, the pattern is typically if Dispose is called on an object, and that
object has references to other objects that implement IDisposable, then it
disposes of those objects.

If you have a reference to an implementation of IDisposable and call it,
and another class has a reference to it, that parent class will not be (nor
should it be) disposed of.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"steve bull" <bu****@comcast.net> wrote in message
news:Oa**************************@4ax.com...

If I have two classes both of which have unmanaged resources that need to
be cleaned up. For example class B is a child of class A
and I create an instance of class B. Then, if I call B.Dispose - Does
A.Dispose get called automatically or do I have to clean up
the unmanaged resource in class A as well in B.Dispose?

Thanks,

Steve

Nov 17 '05 #2
B.Dispose overrides A.Dispose, so it is B.Dispose's responsibilty to
call:

base.Dispose();

Nov 17 '05 #3


Thanks.
On Fri, 8 Jul 2005 12:29:12 -0400, "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote:
Steve,

No, the pattern is typically if Dispose is called on an object, and that
object has references to other objects that implement IDisposable, then it
disposes of those objects.

If you have a reference to an implementation of IDisposable and call it,
and another class has a reference to it, that parent class will not be (nor
should it be) disposed of.

Hope this helps.


Nov 17 '05 #4

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

Similar topics

3
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...
4
by: RiteshDotNet | last post by:
..net Frame work 1. Dispose Method what it does ? A. who its call / when it calls ? B. Is it fire automatically ? c. When dispose method is call what it does ? D. Release a Object from memory or...
11
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...
1
by: Billy | last post by:
Hello... I'm trying to make a database access class for an asp.net application. When I run my application, the Garbage Collecter doesn't seems to unload the memory attributed to my...
14
by: Jonas | last post by:
Hi! I'm developing the middletiers of an ASP.NET application in VB.NET. I've got a business logic layer in which I would like to perform auditing to a database. Instead of making an auditing...
156
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...
9
by: Geoff Callaghan | last post by:
I have several functions that require doing a New on a local object. If the object is local to a sub or function, do I need to dispose of it, or will it just go away like any other local variable?...
71
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or...
6
by: =?Utf-8?B?VmljdG9y?= | last post by:
Hi, I have a UserControl (C# WinFroms application) that requires some cleaning code to run when a form with control is closed. Where I can put that code? There is no ControlClosing or...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.