Why dtor are not executed when removing a struct from associative arrays?

Steven Schveighoffer schveiguy at gmail.com
Mon Sep 20 18:13:53 UTC 2021


On 9/20/21 10:22 AM, Tejas wrote:
> In case you still want to delete stuff deterministically despite what 
> Steve said, I suggest you make your `struct` a reference and use 
> `core.memory.__delete`(not recommended to use this carelessly, btw)

Do not call `__delete` here, use `destroy`. `__delete` will attempt to 
deallocate the block, which likely will fail since the key comes before 
the value, and GC.free on an interior pointer (I think) fails.

But if it succeeded, it would not be good. This leaves a dangling 
pointer inside the AA. Rehashing the AA likely would result in a memory 
corruption.

If you use destroy, the destructor will be called by the GC as well, but 
a struct should properly handle destroying the .init value.

-Steve


More information about the Digitalmars-d-learn mailing list