[Issue 23318] New: GCAllocator should not implement deallocate
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Fri Sep 2 15:38:44 UTC 2022
https://issues.dlang.org/show_bug.cgi?id=23318
Issue ID: 23318
Summary: GCAllocator should not implement deallocate
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: enhancement
Priority: P1
Component: phobos
Assignee: nobody at puremagic.com
Reporter: snarwin+bugzilla at gmail.com
Currently, a container instantiated using GCAllocator is not usable in @safe
code:
---
/+dub.sdl:
dependency "emsi_containers" version="~>0.9.0"
+/
import containers.dynamicarray;
import std.experimental.allocator.gc_allocator;
void main() @safe
{
auto a = DynamicArray!(int, GCAllocator)();
}
---
Attempting to compile the above program produces the following error message:
---
onlineapp.d(9,10): Error: `@safe` function `D main` cannot call `@system`
destructor `containers.dynamicarray.DynamicArray!(int, GCAllocator,
false).DynamicArray.~this`
---
The destructor is @system because it calls GCAllocator.deallocate, which in
turn calls GC.free [1]--in other words, because the container is freeing its
memory manually, rather than relying on the GC to free it automatically.
Since GCAllocator is intended to be std.experimental.allocator's interface to
D's built-in GC, it should match the behavior of the GC as closely as possible.
In particular, a container instantiated with GCAllocator should behave the same
way as a container that allocates with `new` directly w.r.t. memory management.
Such a container would *not* free its memory manually on destruction.
To resolve this inconsistency, GCAllocator.deallocate should be removed.
[1]
https://github.com/dlang/phobos/blob/v2.100.1/std/experimental/allocator/gc_allocator.d#L89-L93
--
More information about the Digitalmars-d-bugs
mailing list