Hi
when do you use destructors and what are the advantages /disadvantages?
ch Jim 3 2100
"Jimmy" <be****@gmail.c om> wrote in message
news:Ob******** ******@TK2MSFTN GP15.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
but what are the disadvantages if any?
ch Jim
"Mythran" <ki********@hot mail.comREMOVET RAIL> wrote in message
news:Ou******** ******@TK2MSFTN GP15.phx.gbl... "Jimmy" <be****@gmail.c om> wrote in message news:Ob******** ******@TK2MSFTN GP15.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
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 ObjectDisposedE xception
when members are accessed on a disposed custom class. You don't see this done too often :)
If you inherit from System.Componen tModel.Componen t, 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..jwaon line..com
-----------------------------------------------------------------------
"Jimmy" <be****@gmail.c om> wrote in message news:eZ******** ******@TK2MSFTN GP15.phx.gbl... but what are the disadvantages if any?
ch Jim "Mythran" <ki********@hot mail.comREMOVET RAIL> wrote in message news:Ou******** ******@TK2MSFTN GP15.phx.gbl... "Jimmy" <be****@gmail.c om> wrote in message news:Ob******** ******@TK2MSFTN GP15.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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 an idea that we can have private constructors and destructors
but am not able to find a situation where they can be used...
Regards
RVG
rajeshgarg@opussoft.com
|
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
Nuno Barros
|
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 standard says that empty() has constant complexity. If it actually
called the destructor for each object, it seems to me it would have
linear complexity. Does empty() call the destructor for each object in the
container? If yes, why is it described...
|
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!
|
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
destructors.
What I am expecting is, if the destructor is declared as virtual in
base, the destructors of all its derived classes also should be
virtual always.
What exactly is the reason for not having it so?
| |
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 (and rarely). This always happens during
process shutdown, after some thread has called System.Environment.Exit(). Clearly,
some sort of race condition.
Note that what follows only applies to destructors that are called when the process
shuts...
|
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 class is created, I
dynamically allocate an object of the class with the legacy C++ code.
However because the __value class has no destructor, I can never release
that allocated memory. Why does a __value class allow no destructor ?
Without it I...
|
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;
}
|
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.
|
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
{
blah();
virtual ~blah();
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
| |
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |