Calling destroy on struct pointer

Radu via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Feb 25 02:44:07 PST 2017


On Saturday, 25 February 2017 at 08:36:02 UTC, Ali Çehreli wrote:
> On 02/25/2017 12:17 AM, Radu wrote:
>
> > destroy(cc) -> does c = C.init
> > destroy(*cc); -> calls the C dtor
> >
> > Is this by design? If so - how can I destroy and get the dtor
> called
> > without dereferencing the pointer?
>
> It's by design because setting a pointer to null can be 
> considered as destroying the pointer. Dereferencing is the 
> right way of destroying the object through the pointer.
>
> I had added the following warning after somebody else was burnt 
> by this feature. :)
>
>   http://ddili.org/ders/d.en/memory.html#ix_memory.destroy
>
> Ali

I think this is BAD. Why?

- it is one of those WAT?? moments that brings a RTFM slap to 
you. The defaults should not be surprising, and in this case 
straight dangerous as it can lead to leaks.
- it is not always possible to dereference the pointer, think 
some circular structures where deref would get you one of those 
fwd. declaration errors.
- the deprecated delete will call the dtor, destroy is suppose to 
replace delete - hence it should work the same.

In my opinion destroy should do this:
- call dtor if the pointer type has one defined
- nullify the pointer

This is what I was expecting anyhow to happen...



More information about the Digitalmars-d-learn mailing list