473,396 Members | 1,965 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,396 software developers and data experts.

Disposing. My objects won't destroy


How do I make sure Ive disposed of my objects please?
Ive added interface IDispose to a base class.
Ive several sub classes inheriting from this base class.
I store one or other of these subclasses in a variable and call dispose(),
but the Dispose() method of the base class is called rather than in the sub
class

example

class baseclass : IDisposable
{
public void Dispose()
{
this is always called
}
}

class SubClass:baseclass
{
public void Dispose()
{
this is never called
}
}
baseclass aclass = null;
SubClass anotherclass = new SubClass();

aclass = anotherclass;
aclass.Dispose();<< this calls baseclass.dispose rather than
subclass.dispose;
Nov 16 '05 #1
7 6295
You're forgetting your virtual and override keywords.

class baseclass : IDisposable
{
public virtual void Dispose()
{
// this is always called
}
}

class SubClass:baseclass
{
public override void Dispose()
{
base.Dispose();

// This is always called
}
}

"Claire" <cc@hotmai1.com> wrote in message
news:37*************@individual.net...

How do I make sure Ive disposed of my objects please?
Ive added interface IDispose to a base class.
Ive several sub classes inheriting from this base class.
I store one or other of these subclasses in a variable and call
dispose(), but the Dispose() method of the base class is called rather
than in the sub class

example

class baseclass : IDisposable
{
public void Dispose()
{
this is always called
}
}

class SubClass:baseclass
{
public void Dispose()
{
this is never called
}
}
baseclass aclass = null;
SubClass anotherclass = new SubClass();

aclass = anotherclass;
aclass.Dispose();<< this calls baseclass.dispose rather than
subclass.dispose;

Nov 16 '05 #2
Sean Hederman <us***@blogentry.com> wrote:
You're forgetting your virtual and override keywords.


<snip>

And the compiler is probably telling you that, with a warning:

"warning CS0108: The keyword new is required on
'SubClass.Dispose()' because it hides inherited member
'baseclass.Dispose()'

Whenever you get a warning, you should read it, understand it, and make
absolutely sure you want to do what you're doing. You can usually
(almost always, actually) get rid of warnings by making the code
clearer.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

Also,

IDispose works with the using keyword. You can explicitly control object
lifetime using 'using':

using (SubClass anotherclass = new SubClass())
{
}

Dispose will be called when control leaves the code block defined by 'using'
- even if an exception is thrown...

--Richard
"Claire" wrote:

How do I make sure Ive disposed of my objects please?
Ive added interface IDispose to a base class.
Ive several sub classes inheriting from this base class.
I store one or other of these subclasses in a variable and call dispose(),
but the Dispose() method of the base class is called rather than in the sub
class

example

class baseclass : IDisposable
{
public void Dispose()
{
this is always called
}
}

class SubClass:baseclass
{
public void Dispose()
{
this is never called
}
}
baseclass aclass = null;
SubClass anotherclass = new SubClass();

aclass = anotherclass;
aclass.Dispose();<< this calls baseclass.dispose rather than
subclass.dispose;

Nov 16 '05 #4
Thanks all, that fixed the Dispose problem :o)

I think what confused me about this is that there WAS a compiler complaint
about me not adding a Dispose method to the object when I added the
IDisposable interface, but there was NO complaint when I didn't make that
method virtual (or not). Doesn't it matter whether it's declared as virtual
or not?

I've also got a class destructor in addition to a dispose. I don't actually
use it because my cleanup code is in the Dispose() method but I notice that
the breakpoint I placed in there wasn't getting called (code line I added to
destructor for testing was timer = null) So how can I be sure my object has
been destroyed?
Nov 16 '05 #5
> I've also got a class destructor in addition to a dispose. I don't
actually use it because my cleanup code is in the Dispose() method but I
notice that the breakpoint I placed in there wasn't getting called (code
line I added to destructor for testing was timer = null) So how can I be
sure my object has been destroyed?


I noticed that the final destructor only got called when the application
terminated. As I'd created/disposed of several instances of the object
during the course of running the app, there were several breaks in the
destructor right at the end. So something still isn't right.
Nov 16 '05 #6
Unlike many other environments .NET does not have deterministic
finalization. Your objects are not neccesarily destroyed when all references
to them are released. That is simply the earliest possible time at which
they can be destroyed. The .NET Garbage Collector will decide when to
destroy your objects, and the destructor will be called then. This is why we
have Dispose, to ensure that expensive resources like database connections
and handles can be closed at a predictable and early time.

For managed resources it is generally unneccesary to implement both a
destructor and a Dispose method. For unmanaged resources see this
http://msdn.microsoft.com/library/de...posemethod.asp
for some reccommendations.

"Claire" <cc@hotmai1.com> wrote in message
news:37*************@individual.net...
I've also got a class destructor in addition to a dispose. I don't
actually use it because my cleanup code is in the Dispose() method but I
notice that the breakpoint I placed in there wasn't getting called (code
line I added to destructor for testing was timer = null) So how can I be
sure my object has been destroyed?


I noticed that the final destructor only got called when the application
terminated. As I'd created/disposed of several instances of the object
during the course of running the app, there were several breaks in the
destructor right at the end. So something still isn't right.

Nov 16 '05 #7
Also have a look at
http://blogs.msdn.com/arich/archive/...23/233683.aspx

"Claire" <cc@hotmai1.com> wrote in message
news:37*************@individual.net...
I've also got a class destructor in addition to a dispose. I don't
actually use it because my cleanup code is in the Dispose() method but I
notice that the breakpoint I placed in there wasn't getting called (code
line I added to destructor for testing was timer = null) So how can I be
sure my object has been destroyed?


I noticed that the final destructor only got called when the application
terminated. As I'd created/disposed of several instances of the object
during the course of running the app, there were several breaks in the
destructor right at the end. So something still isn't right.

Nov 16 '05 #8

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

Similar topics

6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
4
by: pachanga | last post by:
After you destroy a Object and its send to the garbage collections, can you retrieve the object back? Also, if you can, can you destroy an object permantly with no trace of it?
7
by: Adam | last post by:
I have a managed cpp wrapper. Im using this in a native dll as a static variable. I need to free this library when the dll is done being used. The perfect place to do this is DllMain for...
8
by: vvenk | last post by:
Hello: I just wrote my first ASP.Net application. It worked fine on my machine and when I put into production, the ASP.Net process reaches 50% quite fast and then the system does not work...
2
by: Robin Tucker | last post by:
Hi there, Do I need to call "Dispose" on System.Drawing.Brush and System.Drawing.Pen if I've created new ones? What if I'm using stock brushes and pens? Also, do I need to explicitly call...
31
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are...
4
by: Juergen-Bernhard Adler | last post by:
Hello, pretend some noob has (in a fake-static class) provided the following method public static kill_object($obj) { if (!is_object($obj)) return false;
41
by: =?Utf-8?B?VGltIE1hcnNkZW4=?= | last post by:
Hi, I am after suggestions on the best practice declaring and destroying objects. some example code: Private Sub MySub Dim frmMyForm As MyForm Try
29
by: Jerry Spence1 | last post by:
I'm rather confused as to whether something should be disposed of, or not. What is the general rule? How can you be sure of doing the right thing? I've heard about disposing unmanaged resources but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.