473,394 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Destructors

Hi

when do you use destructors and what are the advantages /disadvantages?

ch Jim
Nov 17 '05 #1
3 2078

"Jimmy" <be****@gmail.com> wrote in message
news:Ob**************@TK2MSFTNGP15.phx.gbl...
Hi

when do you use destructors and what are the advantages /disadvantages?

ch Jim


Use destructors anytime you need to cleanup when the class gets destroyed.
Basically, you would implement the IDisposable interface's Dispose() method.
Normally, cleanup includes closing any open files, closing database
connections, cleaning up unmanaged resources, et cetera.

HTH,
Mythran

Nov 17 '05 #2
but what are the disadvantages if any?

ch Jim
"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:Ou**************@TK2MSFTNGP15.phx.gbl...

"Jimmy" <be****@gmail.com> wrote in message
news:Ob**************@TK2MSFTNGP15.phx.gbl...
Hi

when do you use destructors and what are the advantages /disadvantages?

ch Jim
Use destructors anytime you need to cleanup when the class gets destroyed.
Basically, you would implement the IDisposable interface's Dispose()

method. Normally, cleanup includes closing any open files, closing database
connections, cleaning up unmanaged resources, et cetera.

HTH,
Mythran

Nov 17 '05 #3
Dispose is not a destructor.

A disadvantage of using a Destructor is that it does not allow for explicit clean-up of an object at runtime. The Dispose method is
a way of saying, "Hey, clean me up before you go!" to consuming classes. This allows for the explicit and timely release of
unmanaged resources such as Database connections and File references, etc. when the consuming classes no longer require the object
reference.

Once "Disposed", an object should be rendered unusable but it's up to the designer to explicitly throw an ObjectDisposedException
when members are accessed on a disposed custom class. You don't see this done too often :)

If you inherit from System.ComponentModel.Component, you receive a base implementation that prevents the GC (Garbage Collector) from
destroying your object without calling the virtual Dispose method first. This way, all of your cleanup code can be located in one
place. Neat.

I'm sure there are more disadvantages, but I can't think right now -- it's late.

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Jimmy" <be****@gmail.com> wrote in message news:eZ**************@TK2MSFTNGP15.phx.gbl...
but what are the disadvantages if any?

ch Jim
"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:Ou**************@TK2MSFTNGP15.phx.gbl...

"Jimmy" <be****@gmail.com> wrote in message
news:Ob**************@TK2MSFTNGP15.phx.gbl...
> Hi
>
> when do you use destructors and what are the advantages /disadvantages?
>
> ch Jim
>
>


Use destructors anytime you need to cleanup when the class gets destroyed.
Basically, you would implement the IDisposable interface's Dispose()

method.
Normally, cleanup includes closing any open files, closing database
connections, cleaning up unmanaged resources, et cetera.

HTH,
Mythran


Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
3
by: Nuno Barros | last post by:
Cn someone tell me if when i call the destructor of a derivated class, the destructor of the base class is called implicitly? Or shall i call the destructor by myself? Thanks in advance ...
12
by: Ross Boylan | last post by:
I am trying to understand under what circumstances destructors get called with std::vector. I have an application in which I will put real objects, not just pointers, in the vector. 1. The...
8
by: johny smith | last post by:
If I have a simple class with say a couple of integers only is there any need for me to provide a destructor? thanks!
7
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just wondering, why the same concept was not used for the...
26
by: Michi Henning | last post by:
I've been having problem with destructors in the context of having ported C# code developed under .NET to Mono. What happens is that, on a dual-CPU machine, various parts of the code crash randomly...
8
by: Edward Diener | last post by:
I have a __value class which uses some legacy C++ code. So I wrapped the legacy C++ code in another __nogc class and have a pointer to that class as a member of my __value class. When the __value...
3
by: alex.gman | last post by:
If I have code like this int f() { // ... stuff ... g(); if(x > 0) return (x+4); // ... more stuff ... always_call(z); return y; }
6
by: mlw | last post by:
Could someone explain why there is no destructor in Java classes? There are many times you need to be called WHEN an object goes out of scope and not when it will eventally be freed.
6
by: Jeff Newman | last post by:
Hello, Could anyone explain to me why the following class's destructor shows up as having multiple branches? (At least as judged by gcov 4.1.2 when compiled with gcc 4.1.2 ): struct blah {...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.