Disposing. My objects won't destroy

C

Claire

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;
 
S

Sean Hederman

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
}
}
 
J

Jon Skeet [C# MVP]

Sean Hederman said:
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.
 
G

Guest

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
 
C

Claire

Thanks all, that fixed the Dispose problem :blush:)

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?
 
C

Claire

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

Sean Hederman

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/d...guide/html/cpconimplementingdisposemethod.asp
for some reccommendations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top