How to use destroy and free.

Ali Çehreli acehreli at yahoo.com
Mon Apr 25 02:07:50 UTC 2022


On 4/24/22 17:26, Salih Dincer wrote:

 > first destroy() then free()...

Makes sense only if we allocated the memory.

 > import object: doDestroy = destroy;

I like adding 'do' to verbs that can be confused with nouns. For 
example, because 'copy' is both a noun and a verb, I think it helps when 
we name a function as 'doCopy'. However, because 'destroy' is already a 
verb, I would leave it alone. :)

 >    MEM.free(i.ptr);
 >    // You don't need to addrOf(cast(void*)i)

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



More information about the Digitalmars-d-learn mailing list