PC Review


Reply
Thread Tools Rate Thread

Disposing. My objects won't destroy

 
 
Claire
Guest
Posts: n/a
 
      16th Feb 2005

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;


 
Reply With Quote
 
 
 
 
Sean Hederman
Guest
Posts: n/a
 
      16th Feb 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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;
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      16th Feb 2005
Sean Hederman <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?Utf-8?B?UmljaGFyZA==?=
Guest
Posts: n/a
 
      16th Feb 2005

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

 
Reply With Quote
 
Claire
Guest
Posts: n/a
 
      17th Feb 2005
Thanks all, that fixed the Dispose problem )

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?


 
Reply With Quote
 
Claire
Guest
Posts: n/a
 
      17th Feb 2005
> 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.


 
Reply With Quote
 
Sean Hederman
Guest
Posts: n/a
 
      17th Feb 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>> 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.



 
Reply With Quote
 
Sean Hederman
Guest
Posts: n/a
 
      17th Feb 2005
Also have a look at
http://blogs.msdn.com/arich/archive/...23/233683.aspx

"Claire" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>> 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.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Disposing Objects? Adam Right Microsoft C# .NET 4 18th Oct 2006 05:37 PM
Disposing Objects Ken Getz Microsoft Dot NET Framework Forms 4 12th Oct 2005 05:11 AM
Disposing objects =?Utf-8?B?Sk1BcnJveWF2ZQ==?= Microsoft Dot NET Framework 7 23rd Jul 2004 04:21 PM
disposing of objects James Brett Microsoft VB .NET 2 10th Mar 2004 01:21 PM
disposing of objects James Brett Microsoft ASP .NET 1 10th Mar 2004 10:23 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:56 PM.