How to use destroy and free.

frame frame86 at live.com
Mon Apr 25 23:02:53 UTC 2022


On Monday, 25 April 2022 at 02:07:50 UTC, Ali Çehreli wrote:
> >      import core.memory: GC;
>      GC.free(GC.addrOf(cast(void *)(i.ptr)));
> That is wrong because you did not allocate that address 
> yourself.

Hmm? The GC did allocate here(?)

> On 4/24/22 17:26, Salih Dincer wrote:
>
> >    MEM.free(i.ptr);
> >    // You don't need to addrOf(cast(void*)i)

Wrong.

>
> Good point about i.ptr but that free() does not or should not 
> do anything because it is "memory not originally allocated by 
> this garbage collector":
>
>   https://dlang.org/phobos/core_memory.html#.GC.free
>
> Well... maybe it was allocated by that garbage collector and 
> may be it points to the beginning of an allocated block but we 
> don't know that. I wouldn't call free() on an array's memory.
>
> Ali

And if it was, the freeing must be done with `GC.addrOf` or it 
will fail with larger arrays.

You will need the GC address to free the block. That is what 
`__delete` actually does - which was patched back recently, 
reported by Adam: https://issues.dlang.org/show_bug.cgi?id=21550


More information about the Digitalmars-d-learn mailing list