473,606 Members | 3,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:basecl ass
{
public void Dispose()
{
this is never called
}
}
baseclass aclass = null;
SubClass anotherclass = new SubClass();

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

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

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

// This is always called
}
}

"Claire" <cc@hotmai1.com > wrote in message
news:37******** *****@individua l.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:basecl ass
{
public void Dispose()
{
this is never called
}
}
baseclass aclass = null;
SubClass anotherclass = new SubClass();

aclass = anotherclass;
aclass.Dispose( );<< this calls baseclass.dispo se rather than
subclass.dispos e;

Nov 16 '05 #2
Sean Hederman <us***@blogentr y.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.Dispo se()' because it hides inherited member
'baseclass.Disp ose()'

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.co m>
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:basecl ass
{
public void Dispose()
{
this is never called
}
}
baseclass aclass = null;
SubClass anotherclass = new SubClass();

aclass = anotherclass;
aclass.Dispose( );<< this calls baseclass.dispo se rather than
subclass.dispos e;

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 reccommendation s.

"Claire" <cc@hotmai1.com > wrote in message
news:37******** *****@individua l.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******** *****@individua l.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
2561
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 "SEGV" when run (presumably - attempt to delete deleted memory. Please take a look and see if you can notice any mistakes I'm making. Basically, I want to store classes of my objects in a vector. I also have three further questions:
4
1901
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
2418
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 DLL_PROCESS_DETACH, but I can't do this when touching managed code, even if its just calling "delete someObject;". Any recommendations on how to free this object (it needs to be freed when the dll is being unloaded from its caller). Since the CRT is...
8
1942
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 anymore until I kill that process. Obviously, this is not acceptable. Looking back, I do not destroy any objects in my form. Would that be the reasn why the application breaks down?
2
2433
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 "Dispose" on Graphics too? In all cases in my code, they are local variables anyway. Won't VB.NET dispose them automatically when the function exits? Thanks for any tips,
31
3168
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 some of the most powerful programming tools I have found. Often times as my program grows so do my objects. Often times I look back and see that my objects could be broken down int several smaller more re-usable module pieces of code. Is it a...
4
2024
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
1865
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
2359
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 what is an unmanaged resource? Does everything need disposing? I've been using Webrequest, Webresponse, Streamreaders etc. How can I be sure of what needs disposing? Do these get automatically get disposed of at the end of the procedure...
0
8448
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8126
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6796
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...
0
5470
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
3948
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
4010
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2454
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1572
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1313
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.