www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How do I do placement delete in D?

reply "Craig Black" <craigblack2 cox.net> writes:
I know emplace is used to do placement new.  What is the equivalent 
placement delete?

-Craig 
Dec 14 2010
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 14 Dec 2010 15:18:32 -0500, Craig Black <craigblack2 cox.net>  
wrote:

 I know emplace is used to do placement new.  What is the equivalent  
 placement delete?

x = emplace(yourAllocationFunction!T()); ... clear(x); yourDeallocationFunction(x); // if necessary. For instance, stack data doesn't need to be deallocated -Steve
Dec 14 2010
next sibling parent reply "Craig Black" <craigblack2 cox.net> writes:
"Steven Schveighoffer" <schveiguy yahoo.com> wrote in message 
news:op.vnpz6ob6eav7ka steve-laptop...
 On Tue, 14 Dec 2010 15:18:32 -0500, Craig Black <craigblack2 cox.net> 
 wrote:

 I know emplace is used to do placement new.  What is the equivalent 
 placement delete?

x = emplace(yourAllocationFunction!T()); ... clear(x); yourDeallocationFunction(x); // if necessary. For instance, stack data doesn't need to be deallocated -Steve

Is there a way to explicitly invoke a struct destructor without doing an explicit delete? -Craig
Dec 14 2010
next sibling parent reply "Craig Black" <craigblack2 cox.net> writes:
Thanks Steve.  clear seems to be calling the destructor twice.  Is this 
intentional?

-Craig 
Dec 14 2010
next sibling parent "Craig Black" <craigblack2 cox.net> writes:
 No.  Can you file a bug?
 
 -Steve

done
Dec 14 2010
prev sibling parent reply "Craig Black" <craigblack2 cox.net> writes:
Arg!  Someone removed my bug report and assumed I was wrong about it without 
even checking into it.  Why do I even try to help the D community? 
Dec 14 2010
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 12/14/10 5:09 PM, Craig Black wrote:
 Arg! Someone removed my bug report and assumed I was wrong about it
 without even checking into it. Why do I even try to help the D community?

The bug report stands, thanks for clarifying it. Andrei
Dec 14 2010
parent "Craig Black" <craigblack2 cox.net> writes:
"Andrei Alexandrescu" <SeeWebsiteForEmail erdani.org> wrote in message 
news:ie8vpc$2gbg$1 digitalmars.com...
 On 12/14/10 5:09 PM, Craig Black wrote:
 Arg! Someone removed my bug report and assumed I was wrong about it
 without even checking into it. Why do I even try to help the D community?

The bug report stands, thanks for clarifying it. Andrei

Thanks!
Dec 14 2010
prev sibling parent "Craig Black" <craigblack2 cox.net> writes:
About the destructor being called twice, perhaps clear is passing by value 
rather than by reference?  If so, is this desirable?

-Craig 
Dec 14 2010
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 14 Dec 2010 15:33:26 -0500, Craig Black <craigblack2 cox.net>  
wrote:

 "Steven Schveighoffer" <schveiguy yahoo.com> wrote in message  
 news:op.vnpz6ob6eav7ka steve-laptop...
 On Tue, 14 Dec 2010 15:18:32 -0500, Craig Black <craigblack2 cox.net>  
 wrote:

 I know emplace is used to do placement new.  What is the equivalent  
 placement delete?

x = emplace(yourAllocationFunction!T()); ... clear(x); yourDeallocationFunction(x); // if necessary. For instance, stack data doesn't need to be deallocated -Steve

Is there a way to explicitly invoke a struct destructor without doing an explicit delete?

Not sure what you mean. All clear is going to do is invoke the destructor, so I don't know what else you need? -Steve
Dec 14 2010
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 14 Dec 2010 16:05:14 -0500, Craig Black <craigblack2 cox.net>  
wrote:

 Thanks Steve.  clear seems to be calling the destructor twice.  Is this  
 intentional?

No. Can you file a bug? -Steve
Dec 14 2010
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 14 Dec 2010 18:09:44 -0500, Craig Black <craigblack2 cox.net>  
wrote:

 Arg!  Someone removed my bug report and assumed I was wrong about it  
 without even checking into it.  Why do I even try to help the D  
 community?

Nobody removed your report, I'm not sure why you think that. I hope you don't stop trying to improve D, we need everyone's support who is willing to give it. -Steve
Dec 15 2010
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 15 Dec 2010 12:46:34 -0500, Stanislav Blinov <blinov loniir.ru>  
wrote:

 15.12.2010 0:05, Craig Black пишет:
 Thanks Steve.  clear seems to be calling the destructor twice.  Is this  
 intentional?

 -Craig

there has been discussion on how to properly implement clear(). Initial implementation calls dtor, then calls default ctor to leave the object in a meaningful state for the GC to consume. Therefore, when object is being collected, dtor gets called for a second time. The discussion ended on the proposal of nullifying objec't vtbl instead of calling default ctor, which will obviate the need of a second dtor call, but I think this is not currently implemented.

No, the issue is different. Struct dtors are not called when GC memory is collected. I found the issue. I just checked in a fix: http://www.dsource.org/projects/druntime/changeset/451 -Steve
Dec 15 2010
prev sibling parent Stanislav Blinov <blinov loniir.ru> writes:
15.12.2010 0:05, Craig Black пишет:
 Thanks Steve.  clear seems to be calling the destructor twice.  Is 
 this intentional?

 -Craig

there has been discussion on how to properly implement clear(). Initial implementation calls dtor, then calls default ctor to leave the object in a meaningful state for the GC to consume. Therefore, when object is being collected, dtor gets called for a second time. The discussion ended on the proposal of nullifying objec't vtbl instead of calling default ctor, which will obviate the need of a second dtor call, but I think this is not currently implemented.
Dec 15 2010