Why is "delete" unsafe?

mw mingwu at gmail.com
Wed Sep 23 04:15:51 UTC 2020


On Saturday, 27 October 2012 at 01:08:12 UTC, Jonathan M Davis 
wrote:
> 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.

What do you mean by saying "it's definitely not safe" here?

I mean: if I'm careful and know what I'm doing, e.g. remove all 
the reference to  any part of the `object` before call 
core.memory.GC.free(object), is there still any inherit "unsafe" 
side of `free` I should be aware of?

FYI: I just described my use case here:

https://forum.dlang.org/post/hzryuifoixwwywwifwbz@forum.dlang.org



More information about the Digitalmars-d-learn mailing list