Re: Dispose, Dispose(true), Finalize, IDisposable

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



tascien@xxxxxxxxx wrote:
I am getting confused about the role of any of the above methods and
interfaced. But what I want to do is simple... the Destructor such as
the one in C#.

You mean as in C++? There is no destructors in .NET, like the ones in C++.

Basically, my only issue, is to make sure the Data Base connection
inside my class, DBCONN is closed, when the object is out of scope or
destroyed in either way.

You can't do that. There is no reference counting in .NET, so there are no destructors. There is no extra code executed when an object goes out of scope, as there is in a system using reference counting, so there is no way that you can make anything happen when an object goes out of scope.

In dotnet 1.1, the code below used to work. As the page finished
loading, or when db=Nothing, it should have written out:

Connection started
Connection Killed

No, it shouldn't. You have to explicitly call the Dispose method, otherwise it will never be executed. There is nothing magic about the IDisposable interface, it's just an interface as any other.

but in dotnet 2.0, it doesn't.

No, not there either.

am I missing something here? I read the
information at:
http://msdn2.microsoft.com/en-us/library/b1yfkh5e.aspx

but I do not want to completely take away the system's responsibility
to clean up.

You should not leave it to the system at all. You should call the Dispose method so that you have control over when the connection is closed.

Better yet, use a Using statement. That creates a Try ... Finally structure that ensures that the object is disposed whatever happens.

I just want to make sure the connection is closed. the
system should still have the responsibility to clean up.

The only way to make sure that the connection is closed is _not_ to leave the clean up to the system.

Even if you implement a finalizer (which is common when you implement IDisposable), you have no control over when the connection is going to be closed. You don't even have a guarantee that it will ever be closed.

In other words, I want a method that runs when the class is finished
or out of scope.

There is no way of doing that. That's why there is an IDisposable interface.

--
Göran Andersson
_____
http://www.guffa.com
.



Relevant Pages

  • Re: 6SN7 Mu Follower Distortion Paper
    ... the device I have bought is an HP 3581A Wave Meter - it is in effect a spectrum analyser but not with a PC interface for capturing nice pictures. ... You can connect it to a scope but as its scan rate is of the order of seconds you wont see much on a scope unless it is a storage type and mine isn't - it will interface directly to a pen plotter though!!!! ... It is a start and the spectral shape of the harmonics is the second step. ...
    (rec.audio.tubes)
  • Re: 6SN7 Mu Follower Distortion Paper
    ... the device I have bought is an HP 3581A Wave Meter - it is in effect a spectrum analyser but not with a PC interface for capturing nice pictures. ... You can connect it to a scope but as its scan rate is of the order of seconds you wont see much on a scope unless it is a storage type and mine isn't - it will interface directly to a pen plotter though!!!! ... Could not get either one of them to work properly - probably something to do with the sound chip but it is a new laptop. ...
    (rec.audio.tubes)
  • Re: 6SN7 Mu Follower Distortion Paper
    ... Ian. ... the device I have bought is an HP 3581A Wave Meter - it is in effect a spectrum analyser but not with a PC interface for capturing nice pictures. ... You can connect it to a scope but as its scan rate is of the order of seconds you wont see much on a scope unless it is a storage type and mine isn't - it will interface directly to a pen plotter though!!!! ...
    (rec.audio.tubes)
  • Re: C++ interface
    ... > destructor because they don't exist for the interface. ... > constructors, destructor and etc. ... If you can somehow be sure no derived class will have a destructor ...
    (comp.lang.cpp)
  • Re: Semantics and STL/DTL
    ... > scope conforms to the minimal interface used within my local scope. ... > then why should my local scope enforce the full Stack interface ...
    (comp.object)