|
|
||||||
|
#1
|
|
|
|
|
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; |
|
|
|
#2
|
|
|
|
|
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> wrote in message news:k6u1 [..] |
|
#3
|
|
|
|
|
Sean Hederman <usemy> 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. |
|
#4
|
|
|
|
|
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: [..] |
|
#5
|
|
|
|
|
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? |
|
#6
|
|
|
|
|
> 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. |
|
#7
|
|
|
|
|
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> wrote in message news:05u1 [..] |
|
#8
|
|
|
|
|
Also have a look at
http://blogs.msdn.com/arich/archive/...23/233683.aspx "Claire" <cc> wrote in message news:05u1 [..] |
|
|
| Similar Threads | |
| Disposing objects 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... |
|
| Disposing Objects Suppose I have a Function/Procedure like this: Sub DoSomething() Dim db As New SqlClient.SqlConnection(ConnectionString) db.Open() Dim cmd As New... |
|
| Disposing of Objects -AGAIN I don't think I clearly stated my problem in my prior question so let me try again. I have the following: Private myobject as Bitmap myobject = mybitmap (created from a... |
|
| Disposing of Objects. I have a global variable that is of type Image. I set this to different images in different routins using BitBlt to copy from the screen. If I dispose of the variable image... |
|
| Disposing objects Hi, im have a trouble with the memory, my app is to big and it consumes a LOT of memory, i read the posts of dispose in the forum, and all recomends to leave the GC do this... |
|
|
All times are GMT. The time now is 11:25 PM. | Privacy Policy
|