Question about Object.destroy

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Sep 20 12:06:02 PDT 2015


On Sunday, 20 September 2015 at 18:52:17 UTC, Lambert Duijst 
wrote:
> Just want to know if D protects against dangling pointers or is 
> this just something you should never do.

The answer is both: it tries to protect you but you still 
shouldn't do it.

> If we are not supposed to use Object.destroy anymore then how 
> can we still free non-memory resources that are held by classes 
> (which are typically cg'ed) in a deterministic way ?

The function btw is actually destroy(Object). It works as 
Object.destroy because of the uniform function call syntax 
feature which will rewrite it. But I recommend doing 
destroy(Object) because then you get consistent results, even if 
an interface has its own destroy method.

But you can use it, destroy is cool. delete was teh problem 
because it doesn't provide even the minimal protection the 
destroy function has.

(You can also malloc/free or stack allocate if you really want to 
take matters into your own hands but then the language basically 
doesn't help you at all in the dangling pointer problem.)


More information about the Digitalmars-d-learn mailing list