delete and __delete not equivalent
Steven Schveighoffer
schveiguy at gmail.com
Fri Jan 15 14:38:55 UTC 2021
On 1/15/21 8:59 AM, Adam D. Ruppe wrote:
> On Friday, 15 January 2021 at 13:15:12 UTC, Nick Treleaven wrote:
>> From that issue, it's surprising that delete frees array memory even
>> when the ptr field isn't the start of the allocation.
>
> while(true) {
> int[] a = new int[](whatever);
> delete a;
> }
>
> That will not run out of memory. Change it to `__delete(a);` or
> `GC.free(a.ptr)` like the docs tell you to do and it will.
To clarify, as long as whatever is larger than 2k, it will. And that's a
bug for sure (the reason for this is based on the GC implementation).
> It is true that if you slice a and delete it, it still blasts the whole
> block which might be surprising.... but like plain simple new/delete
> pair actually working certainly isn't surprising and plain simple
> new/__delete pair doing absolutely nothing makes the whole thing pretty
> worthless. Hence the bug report.
I'm unsure if deleting from the middle is expected or desired.
I would first fix it to do what the delete call does, and then possibly
"fix" that behavior, as I wouldn't expect it either. If you pass a slice
into a function, that function probably shouldn't affect everything in
the block from that slice.
But clearly a bug in the first respect.
-Steve
More information about the Digitalmars-d
mailing list