Why is "delete" unsafe?

Jonathan M Davis jmdavisProg at gmx.com
Fri Oct 26 18:08:00 PDT 2012


On Saturday, October 27, 2012 01:09:39 Alex Rønne Petersen wrote:
> On 27-10-2012 01:03, Minas wrote:
> > So the delete keyword has been deprecated - so good bye manual memory
> > management...
> 
> Um, no. Use destroy() from the object module instead.

Definitely, though it's important to note that what it's doing is inherently 
different. It destroys what you pass to it but does _not_ free the memory, 
which is why it's safer.

> To free memory
> from the GC, use core.memory.GC.free().

Yes. But using core.memory.GC.free is unsafe for the same reasons that delete 
is. It's just that it's a druntime function instead of a part of the language, 
so it's less likely for someone to free GC memory without knowing what they're 
doing. It's there because there _are_ times when it makes sense and is useful, 
but it's definitely not safe, so you have to be careful and know what you're 
doing.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list