473,695 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Call Destructor" Vs. "Destruct Object"


If we have a simple class such as follows:

#include <string>

struct MyStruct {

std::string member;

MyStruct(unsign ed 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.me mber.string();
mystruct_obj.My Struct();

(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_cas t<MyStruct&>(*m em);

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
"constructi ng" and "destructin g" 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 3333
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.me mber.string();
mystruct_obj.My Struct();
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 "destructor s" rather than "constructo rs".

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.me mber.string();
mystruct_obj.My Struct();
>
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.me mber.string();
mystruct_obj.My Struct();
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
"Destructin g" 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::~MySt ruct 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_cas t<MyStruct&>(*m em);

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_delet e (X) (X)->~

And so:

placement_delet e (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
46966
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
6736
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 with "first-class airplane ticket". I just find the name a bit strange. Also, if there are first-class objects, what would the second-class objects or economy/tourist class objects be? :)
4
9794
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 object that was added last. Could someone please tell me why this happens ? Am I doing something wrong here ?
4
11157
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 type=button onclick=" function myobject(a) { function c() { return this; } var prop1=a; this.youpla = c(); } var i = new myobject();
22
3330
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
2617
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 ArrayList is currently deserializing in the constructor. When is the correct time to serialize the ArrayList to XML? In C++ I would do this in the destructor. I see C# has a destructor, but I am unsure about all of this IDisposable, Finalize,...
10
2882
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
2052
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 to use elsewhere. For example, suppose you have a class Note. This class stores some text, as a linked list of lines of text. The destructor runs as follows: Note::~Note() {
1
7106
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 someone who bothers to read all of it have some pointers. Note, I have posted the stack trace and the code exhibiting the problem further down so if you want to start by reading that, search for +++ Also note that I am unable to reproduce...
0
9122
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, 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...
0
9001
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8860
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,...
0
7670
agi2029
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...
1
6496
isladogs
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...
0
5841
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();...
0
4587
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2280
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1980
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.