Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Explicitly calling destructor

Reply
Thread Tools

Explicitly calling destructor

 
 
engaarea@gmail.com
Guest
Posts: n/a
 
      09-08-2006
hi all,

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


Regards
5

 
Reply With Quote
 
 
 
 
Markus Grueneis
Guest
Posts: n/a
 
      09-08-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
>

 
Reply With Quote
 
 
 
 
engaarea@gmail.com
Guest
Posts: n/a
 
      09-08-2006

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



Can you give me an example
Thanks

 
Reply With Quote
 
Paul Henderson
Guest
Posts: n/a
 
      09-08-2006
(E-Mail Removed) 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

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      09-08-2006
(E-Mail Removed) 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.

 
Reply With Quote
 
Aman JIANG
Guest
Posts: n/a
 
      09-08-2006
(E-Mail Removed) 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

 
Reply With Quote
 
Moonlit
Guest
Posts: n/a
 
      09-08-2006
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<ISerialize> ) ];

char WRefPtr[ sizeof( MWRefPtr<ISerialize> ) ];

char KeyStroke[ sizeof( MKey ) ];

};



UVar::UVar( const string& String ):

Type ( eString )

{

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

}


void UVar::CleanUp()

{

switch( Type )

{

case eString:

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

break;

//...etc

}

}


Regards, Ron AF Greve

http://moonlit.xs4all.nl

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>
> Markus Grueneis wrote:
>>
>> Yes, after a placement new.
>>
>>
>> best regards,
>> -- Markus
>>

>
>
> Can you give me an example
> Thanks
>



 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      09-08-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it a good practice to call the destructor explicitly and use placement new(this) in assignment operators? Michael Tsang C++ 8 12-14-2009 07:43 PM
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 0 05-19-2008 07:05 PM
compiler generated destructor vs class implemented destructor arun C++ 2 06-13-2006 05:43 AM
Explicit destructor calls from inside base class destructor frs C++ 20 09-21-2005 09:22 AM
When is a good time to explicitly call a destructor ? Timothy Madden C++ 3 11-19-2004 11:06 AM



Advertisments