Why is "delete" unsafe?

Alex Rønne Petersen alex at lycus.org
Fri Oct 26 16:09:39 PDT 2012


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. To free memory 
from the GC, use core.memory.GC.free().

>
> I have read in some threads that delete is an unsafe operation. What
> does this exactly mean? What is unsafe about it? What does it have to do
> with the GC? (if there was no garbage collection, would it be unsafe?)

void* p = malloc(__traits(classInstanceSize, Object));
p[0 .. __traits(classInstanceSize, Object)] = typeid(Object).init[];
Object o = cast(Object)p;
delete o; // Spot the bug.

-- 
Alex Rønne Petersen
alex at lycus.org
http://lycus.org


More information about the Digitalmars-d-learn mailing list