Why is "delete" unsafe?

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Oct 26 16:12:15 PDT 2012


On Sat, Oct 27, 2012 at 01:03:14AM +0200, Minas wrote:
> So the delete keyword has been deprecated - so good bye manual
> memory management...

Um, that's a misconception. If you want manual memory management, use
malloc(), free(), and emplace.


> 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?)

The problem is that you can call delete on GC'd objects, which in some
cases causes bad interaction with the GC. That's why it has been
deprecated.

The intention was never to get rid of manual memory management. It was
to prevent unsafe interactions with the GC. If you don't want to use the
GC, use malloc(), free(), and emplace. (In fact, this way you can have
*both* GC and manual memory management. The emplace()'d objects will be
manually managed, and the others will be collected by the GC.)


T

-- 
All men are mortal. Socrates is mortal. Therefore all men are Socrates.


More information about the Digitalmars-d-learn mailing list