Everything2
Near Matches
Ignore Exact
Full Text
Everything2

Destructor

created by Webster 1913

(thing) by Toxick (1.4 wk) (print)   ?   (I like it!) Sun Oct 14 2001 at 21:06:26

A destructor is a method in Object Oriented Programming that allows the programmer to clean up their mess before they abandon an object.

Yeah, but why would anyone want to do that?

During the life of an object, it may allocate memory, create pointers, references, linked lists, or other constructs which eat memory. Sometimes when you're done with an object and you don't have an adequate destructor, these resources will still be reserved by your program, and therefore not available to other processes. Even your program can't re-grab them. Since your program can no longer use them, and they're off-limits to other programs, these resources are in limbo and are rendered useless. This is called a memory leak.

Memory leaks are generally considered bad juju in programming circles.

In Java there are no explicit destructors, because of the garbage collector, which eliminates the need for the programmer to manually clean up the mess. When the commode is flushed on your object, as it were, the garbage collector wipes your bum for you, blows your nose and offers you a mint. Any resources your program has snarfed up are released back to the heap. All this is done without the knowledge or consent of the programmer.

In Visual Basic there is a combination of manual and automatic destruction. You can't deallocate anything (because you were never allowed to allocate it in the first place), but you can handle any last minute details before your object bites the dust. And if your object was holding any resources, they are dropped before the object is destroyed. This is done through your class's Terminate Function.

In C++, you had better keep track of anything that may need to be released, because there is no garbage collector whatsoever. You're on your own. The destructor is indicated by a bitwise complement operator (~) followed by the constructor's name (get it?). So if your constructor was called "MyObject()", the destructor would be called "~MyObject()".


Addendum Oct. 30, 2002: ariels has just reminded me that destructors are not only for deallocating memory. He gives me the for instance: "the fstream destructor closes the file -- it releases a file descriptor (and, if based on libc, also a file pointer)."

Basically the destructor is for any cleanup operation. I, personally, use it most often for deallocating memory, releasing pointers, and when using sockets, calling the WSACleanup routine.


(definition) by Webster 1913 (print) Tue Dec 21 1999 at 23:01:47

De*struc"tor (?), n. [L., from destruere. See Destroy, and cf. Destroyer.]

A destroyer. [R.]

Fire, the destructor and the artificial death of things.
Boyle.

 

© Webster 1913


De*struct"or, n.

A furnace or oven for the burning or carbonizing of refuse; specif. (Sewage Disposal),

a furnace (called in full refuse destructor) in which the more solid constituents of sewage are burnt. Destructors are often so constructed as to utilize refuse as fuel.

 

© Webster 1913


printable version
chaos

Constructor unwind-protect resource acquisition is initialization RAII
rule of three finalizer delete[] bad juju
Object Orientation in ANSI C Operator overloading ten commandments for c++ programmers Polymorphism
dodgy C++ AZS Reference
Graham Greene Mario Artist virtual method table undefined behavior
linked list PHP virtual function destroy
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Things you could have written:
The Jazz Singer
Godspeed You Black Emperor!
Interstate Numbering System
How to smuggle a snake onto an airplane
Li Bai
Requiem Aeternam
My mom is my dad is my daughter is dead
public executions
Robert Rodriguez
Greek alphabet
Franz Schubert
Slashdot Effect
La Everythingaise
New Writeups
Meezzio
Gotlandssnus(thing)
argv
Astral Plane(idea)
Madara
One Winged Angel(fiction)
Tom Rook
Talk is cheap(poetry)
shaogo
Adelle Davis(person)
Aerobe
race car g sfjsgsd(poetry)
Binah
Dream Log: July 5, 2008(dream)
StrawberryFrog
Forgotten things in space(idea)
antigravpussy
velvet revolution fairy tale(idea)
Heitah
Nerve agent VX(thing)
Pavlovna
shite(idea)
wonton
Days and nights come together in a slow falling down(fiction)
Pavlovna
wee(idea)
katherine
root log: July 2008(log)
Madara
There’s nothing like a trail of blood to find your way back home(fiction)
This page courtesy of The Everything Development Company