On 9/21/21 2:06 AM, Tejas wrote:
On Monday, 20 September 2021 at 18:13:53 UTC, Steven Schveighoffer wrote:
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.


It doesn't succeed when object is stack allocated, otherwise it works; that's why I suggested changing `S` to `S*`.

Oh! I missed that subtle change. I thought you were deleting a pointer to S that lives in a `S[int]`.

I still recommend against this because you can easily get a dangling pointer that way. Aside from that, if you are careful enough not to store a retrieved S* from the aa, you could do that. It will incur one extra allocation per element, which is not ideal.

Using destroy should be the most effective and safest mechanism.

-Steve

Reply via email to