472,989 Members | 2,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

"Call Destructor" Vs. "Destruct Object"


If we have a simple class such as follows:

#include <string>

struct MyStruct {

std::string member;

MyStruct(unsigned const i)
{
member += '0'+i;
}
};

, then using "placement new" to construct an object of the class does the
following:

(1) Calls the constructor of the member object, "member".
(2) Calls the MyStruct constructor for the object itself.

This is conceivibly quite realistically like:

mystruct_obj.member.string();
mystruct_obj.MyStruct();

(i.e. we're invoking the code of two constructors.)

However, take a look at the following life-cycle of a DIY object, paying
particular attention to the way in which we destruct the object:

char *const mem = new char[sizeof(MyStruct)]; /* Allocate the memory */

MyStruct *const p = ::new(mem) MyStruct(5); /* Construct the object */

p->~MyStruct(); /* Destruct the object */

delete [] mem; /* Deallocate the memory */

Notice that when we are destructing the object, it looks as though we're
simply invoking the MyStruct destructor -- but in actuality, we're really
doing:

p->member.~string();
p->~MyStruct();

This is inconsistent with the way in which we construct the object! But not
only that, it's misleading because we are _not_ simply calling the
destructor -- we're doing more than that, we're invoking the constructors
of all base classes and subobjects. For consistency, we should have either
had:

Solution (1) -- Not favourable

char *const mem = new char[sizeof(MyStruct)];

MyStruct &obj = reinterpret_cast<MyStruct&>(*mem);

obj.MyClass(5); /* Construct! */

obj.~MyClass(); /* Destruct! */

delete [] mem;
Solution (2) -- Quite favourable

char *const mem = new char[sizeof(MyStruct)];

MyStruct *const p = ::new(mem) MyClass(5);

::delete(p) MyClass; /* Placement delete! */

The second solution is favourable, as it makes no mention of calling
constructors or destructors, but rather introduces the idea of
"constructing" and "destructing" an object (which may involve the
invocation of one or more constructors/destructors for base classes or
subobjects, etc.). The first solution is downright misleading, because it
resembles exactly the way in which we normally invoke a member function,
and gives no indication that we're also invoking the
constructors/destructors of base classes and subobjects.

My own opinion is that there should be a "placement delete", whose purpose
it is to destruct an object.

The DIY invocation of a destructor via "obj.~Class();" should either:

(1) Be forbidden, as it is for constructors.
(2) Just call _that_ destructor, exactly as if it were a member
function, without invoking any other destructors (such as those of base
objects or subobjects.

Maybe the die is cast... but it's worth a think.

--

Frederick Gotham
Sep 26 '06 #1
5 3268
Frederick Gotham posted:
(1) Calls the constructor of the member object, "member".
(2) Calls the MyStruct constructor for the object itself.

And also the constructors of any of the base objects or member objects of
an std::string.

This is conceivibly quite realistically like:

mystruct_obj.member.string();
mystruct_obj.MyStruct();
This is inconsistent with the way in which we construct the object! But
not only that, it's misleading because we are _not_ simply calling the
destructor -- we're doing more than that, we're invoking the
constructors of all base classes and subobjects.

That should be "destructors" rather than "constructors".

Solution (2) -- Quite favourable

char *const mem = new char[sizeof(MyStruct)];

MyStruct *const p = ::new(mem) MyClass(5);

::delete(p) MyClass; /* Placement delete! */

There should be another statement at the end of that:

delete [] mem;

--

Frederick Gotham
Sep 26 '06 #2
Frederick Gotham wrote:
>
This is conceivibly quite realistically like:

mystruct_obj.member.string();
mystruct_obj.MyStruct();
>
Notice that when we are destructing the object, it looks as though we're
simply invoking the MyStruct destructor -- but in actuality, we're really
doing:

p->member.~string();
p->~MyStruct();
Nope, other way around. The destructors are called in the inverse order
that the constructors were called.
>
This is inconsistent with the way in which we construct the object! But not
only that, it's misleading because we are _not_ simply calling the
destructor -- we're doing more than that, we're invoking the constructors
of all base classes and subobjects. For consistency, we should have either
had:
It's NOT really that inconsistant. The language ALWAYS runs the
constructors for an object in the specified order and ALWAYS runs the
destructors in the inverse of that order. You can NOT pick and chose
which constructors or destructors run or change their ordering.
My own opinion is that there should be a "placement delete", whose purpose
it is to destruct an object.
Perhaps, but I'm not sure what you are trying to accomplish.
>
The DIY invocation of a destructor via "obj.~Class();" should either:

(1) Be forbidden, as it is for constructors.
Yes, and further, the above syntax is almost certainly indicative of a
problem unless obj is a reference.
(2) Just call _that_ destructor, exactly as if it were a member
function, without invoking any other destructors (such as those of base
objects or subobjects.
Ick, that would be a disaster.
Sep 26 '06 #3
Frederick Gotham wrote:
This is inconsistent with the way in which we construct the object! But
not only that, it's misleading because we are _not_ simply calling the
destructor -- we're doing more than that, we're invoking the constructors
of all base classes and subobjects. For consistency, we should have either
had:
What are you proposing? That the language be redefined, years after the
standarization, just because you don't like some syntax?

--
Salu2
Sep 26 '06 #4
Frederick Gotham wrote:
, then using "placement new" to construct an object of the class does the
following:

(1) Calls the constructor of the member object, "member".
(2) Calls the MyStruct constructor for the object itself.
It's the MyStruct constructor which recursively invokes the member
constructors.
This is conceivibly quite realistically like:

mystruct_obj.member.string();
mystruct_obj.MyStruct();
Not really, since explicit constructor calls return new temporary
objects. There is no such thing as an in place constructor call on an
object.
However, take a look at the following life-cycle of a DIY object, paying
particular attention to the way in which we destruct the object:

char *const mem = new char[sizeof(MyStruct)]; /* Allocate the memory */

MyStruct *const p = ::new(mem) MyStruct(5); /* Construct the object */

p->~MyStruct(); /* Destruct the object */

delete [] mem; /* Deallocate the memory */

Notice that when we are destructing the object, it looks as though we're
"Destructing" is awkward; you want "destroying". :)
simply invoking the MyStruct destructor -- but in actuality, we're really
doing:

p->member.~string();
p->~MyStruct();
Not really, it's the MyStruct::~MyStruct which is handling the
recursion to the other destructors.
This is inconsistent with the way in which we construct the object!
Not in the least. The top-level constructor recurses to the constituent
constructors, and the top-level destructor recurses ot the constituent
destructors. I don't see the consistency.
But not only that, it's misleading because we are _not_ simply calling the
destructor -- we're doing more than that, we're invoking the constructors
of all base classes and subobjects.

For consistency, we should have either
had:

Solution (1) -- Not favourable

char *const mem = new char[sizeof(MyStruct)];

MyStruct &obj = reinterpret_cast<MyStruct&>(*mem);

obj.MyClass(5); /* Construct! */
This is stupid because obj is an invalid object with indeterminate
contents.

The placement new syntax makes more sense, because it takes in
uninitialized memory, plus the identity of a class, and puts the two
together.

A constructor is not an ordinary member function of an object. It's a
special member that belongs to the class. The constructor takes memory
which may have indeterminate contents, and turns it into an object.
Solution (2) -- Quite favourable

char *const mem = new char[sizeof(MyStruct)];

MyStruct *const p = ::new(mem) MyClass(5);

::delete(p) MyClass; /* Placement delete! */
This "placement delete" would have to assume that the memory came from
new. But in general, when placement new is used, the memory does not
come from new.

For this placement delete to be useful, it would have to call the
destructor only, and not try to do anything else with the memory. Then
it would be complementary to placement new, which does not allocate
memory.
The second solution is favourable, as it makes no mention of calling
Solution to what problem? That the placement new syntax does not
resemble in-place destruction syntax isn't a problem; it's esthetics.

Construction and destruction are not intended to be symmetrical. What
about this:

{
MyStruct x(3);

}

Where is the symmetry here? Explicit syntax instantiates the object,
yet the destruction is invisible.

Construction takes place in an enviornment in which the object doesn't
exist yet. It's a function that is invoked on the environment to
produce the object. Destruction is something that happens to the
object.

Then there is the fact that C++ is a brutally ugly language with
ill-conceived syntax throughout. If you insist on consistency, don't
use C++.
My own opinion is that there should be a "placement delete", whose purpose
it is to destruct an object.
And there is. You merely don't like its spelling:
``pointer->~Class();'' You'd rather that the parser require "delete
(pointer) Class''. You can do this with the preprocessor:

#define placement_delete (X) (X)->~

And so:

placement_delete (pointer) Class();

now macro-expands to:

(pointer)->~Class();

Hahaha. That shows you how ridiculous and trivial are obsessions for
syntactic sugar.
The DIY invocation of a destructor via "obj.~Class();" should either:

(1) Be forbidden, as it is for constructors.
Invocation of constructors isn't forbidden.
(2) Just call _that_ destructor, exactly as if it were a member
function, without invoking any other destructors (such as those of base
objects or subobjects.
Doh.

Sep 26 '06 #5
Kaz Kylheku posted:
For this placement delete to be useful, it would have to call the
destructor only, and not try to do anything else with the memory. Then
it would be complementary to placement new, which does not allocate
memory.

Yes, I should have written an extra line after that, i.e.:

::delete(p) MyStruct;

delete [] mem;

> (1) Be forbidden, as it is for constructors.

Invocation of constructors isn't forbidden.

Frobidden/Possible/Permitted... all the same concept. All I'm saying is that
you can't do:

obj.ClassName();

to call a constructor.

--

Frederick Gotham
Sep 26 '06 #6

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

Similar topics

3
by: ¤ Alias | last post by:
I have a function named getID3info (lvwDiscInfo.SelectedItem). What is the difference between getID3info (lvwDiscInfo.SelectedItem) and Call getID3info(lvwDiscInfo.SelectedItem) ?
24
by: Hung Jung Lu | last post by:
Hi, Does anybody know where this term comes from? "First-class object" means "something passable as an argument in a function call", but I fail to see the connection with "object class" or...
4
by: Hitesh Bhatiya | last post by:
Hi all, I have written a small program to accept some socket connections, which are then added to a vector (using push_back). But after a few calls to the push_back function, it deleted the...
4
by: Armel Asselin | last post by:
Hello, I'm working on a Javascript interpreter; when I execute this code I cannot figure out why the inner "c" function returns the input widget as "this"... could someone tell ?? <input...
22
by: Dr Duck | last post by:
GDay all, Something seems odd to me.... I wrote a simple C# function public void bind(ref object a, ref object b, bool atob) { if(atob) b = a; else
2
by: Trevor Balcom | last post by:
I have a class which has a member variable of the type System.ArrayList. I plan on having the ArrayList serialize/deserialize itself to/from XML. I have worked out the code to do this and have the...
10
by: Thorsten Ottosen | last post by:
Hi, I'm trying to escape html before its saved in a database. I tried $text = htmlentities( $reader->value ); but that don't work for e.g. japanese characters.
8
by: gw7rib | last post by:
I've been bitten twice now by the same bug, and so I thought I would draw it to people's attention to try to save others the problems I've had. The bug arises when you copy code from a destructor...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.