473,883 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Explicitly calling destructor

hi all,

What are the circumstances in which one would have to call a destructor
explicitly.
Regards
5

Sep 8 '06 #1
7 15650
en******@gmail. com schrieb:
hi all,

What are the circumstances in which one would have to call a destructor
explicitly.
Yes, after a placement new.
best regards,
-- Markus
>
Regards
5
Sep 8 '06 #2

Markus Grueneis wrote:
>
Yes, after a placement new.
best regards,
-- Markus

Can you give me an example
Thanks

Sep 8 '06 #3
en******@gmail. com wrote:
What are the circumstances in which one would have to call a destructor
explicitly.
You should only call a destructor explicitly on an object you have
created using placement new - see the FAQ
[http://www.parashift.com/c++-faq-lite/] section 11.

-- PH

Sep 8 '06 #4
en******@gmail. com wrote:
hi all,

What are the circumstances in which one would have to call a destructor
explicitly.
Usually, the allocation/deallocation of storage and the
construction/destruciton of an object are done together. If you want to
separate those things, you can use the placement new operator to construct
an object into alreaedy allocated storage. The counterpart to that is the
explicit destructor call. It destroys the object, but doesn't deallocate
the storage. This is e.g. used (indirectly through an allocator) by the
standard container classes, like std::vector, because those do their own
memory management.

Sep 8 '06 #5
en******@gmail. com wrote:
hi all,

What are the circumstances in which one would have to call a destructor
explicitly.
Regards
5

When you are in the circumstance that unable to use delete,
Or when you need to manage memory by yourself.
class Dolphin
{
int m_iDummy;
public:
Dolphin() { cout << "Dolphin" << endl; }
virtual ~Dolphin() { cout << "~Dolphin" << endl; }
};

int main()
{
unsigned char buffer [256];

// construct a Dolphin in buffer
Dolphin * d = new(buffer) Dolphin;

// buffer is in stack, not in heap, so you cannot use delete
// But you must destruct it, too:
d->~Dolphin();
}

-----------------------------------------
Dolphin
~Dolphin

Sep 8 '06 #6
Hi,

For example:

Consider a Variant class ( which can hold a number of types like
long/int/string etc)
To save on memory you define a union with all those variables
Now to properly construct a string in the area of the union you would need
placement new
On destruction of your string (maybe because the variant type is assigned a
'long') you have to destruct the string, but not free its memory (it can't
since it is not allocated on the heap, but in the area where the union is
located ) in this case you would explicitely call delete:
Example:

union {

Int8 Char;

Int64 Long;

//UInt8 UChar;

//UInt64 ULong;

double Double;

bool Bool;

char String [ sizeof( std::string ) ];

char Map [ sizeof( std::map<UVar*, UVar*, UFindVar) ];

char SRefPtr[ sizeof( MSRefPtr<ISeria lize) ];

char WRefPtr[ sizeof( MWRefPtr<ISeria lize) ];

char KeyStroke[ sizeof( MKey ) ];

};

UVar::UVar( const string& String ):

Type ( eString )

{

new( this->String ) string( String );

}
void UVar::CleanUp()

{

switch( Type )

{

case eString:

reinterpret_cas t<std::string *const>( String )->~string();

break;

//...etc

}

}
Regards, Ron AF Greve

http://moonlit.xs4all.nl

<en******@gmail .comwrote in message
news:11******** **************@ d34g2000cwd.goo glegroups.com.. .
>
Markus Grueneis wrote:
>>
Yes, after a placement new.
best regards,
-- Markus


Can you give me an example
Thanks

Sep 8 '06 #7
posted:
hi all,

What are the circumstances in which one would have to call a destructor
explicitly.

Most typically when the construction was explicit (e.g. via "placement new"):

char buffer[sizeof(string)]; /* Pretend it's suitably aligned. */

string *const p = ::new(buffer) string("Hello") ;

p->~string();

--

Frederick Gotham
Sep 8 '06 #8

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

Similar topics

1
15274
by: Thomas Ilsche | last post by:
Hi, how do I "explicitly destroy" an Object in PHP5 to make sure the destructor is called an the object destroyed? unset is not an option because there are multiple variables containing the object handle. Waiting for the end of the script is aswell a bad option because the order in which the destructores are called does matter.
14
2429
by: Timothy Madden | last post by:
Hello I have a linked list of object of a class. I thought it would be nice to have the destructor delete the whole list when I delete just the first element. I don't want to recursivly destroy the list elements; the list is arbitrary long. Something like this: class ListElem { char Datum;
3
1751
by: rahul8143 | last post by:
hello, I write a following program and have problem in understanding constructors and destructors. #include <iostream.h> class vector { public: double x; double y;
4
4980
by: Michael | last post by:
Hello, I want to use an object (LowCut) within another object (SampleRateConverter) like it is written as follows: class SampleRateConverter { public: SampleRateConverter( int iSourceSampleRate, int iTargetSampleRate ) {
3
1922
by: marcwentink | last post by:
Say I have a class A, and a class B that inherits from A. Now A (and B) has a virtual destructor and a virtual function F(); If I now make these statements A* ptrA = new B; ptrA->F(); delete ptrA then in the statement ptrA->F(), by means of the polymorph behavior,
6
5646
by: Henrik Goldman | last post by:
Hi I've had a problem with gcc mac osx which I think I figured out. I would like to double check with people here to see if my understanding is correct: I have a class A which class B inherit from. A has a pure virtual function virtual in f1() = 0. In B I implement this function and the compiler works out the code. However from A's destructor B can be compiled and then the runtime aborts with saying that a call to a pure virtual...
2
1481
by: bg_ie | last post by:
Hi, I'm creating objects in my python script belonging to a COM object which I dispatch using win32com.client.DispatchEx. Hence, dllhost.dll is the concerned process. The problem is that the objects destructor within the com object is not called if the object lives past a certain number of seconds. For example, this function will not call the destructor concerned with obj unless the sleep is commented out. def fnction:
2
3751
by: =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
"delete" does two things: 1) Invokes the destructor 2) Deallocates the memory We can manually invoke the destructor with: p->~T(); But is there any way to manually deallocate the memory without invoking
2
3217
by: Studlyami | last post by:
After i create an object can i call the constructor again to reinitialize that objects variables? I know i can create a function to do this, but i was wonder if i can just call the constructor again. If i can call the constructor again is this a good practice or should i avoid such a use of the constructor. Here is some test code. //code created and tested in VS 2003 #include <iostream> using namespace std; class TestClass { public:
0
9777
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,...
0
11109
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
10728
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
10833
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
10405
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 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...
0
7114
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
5980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4602
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.