Bug 18347 – stdx.allocator dispose should be @safe
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-02-01T07:21:07Z
Last change time
2018-02-01T17:26:07Z
Assigned to
No Owner
Creator
Seb
Comments
Comment #0 by greensunny12 — 2018-02-01T07:21:07Z
---
import std.stdio;
void main() @safe
{
import std.experimental.allocator.mallocator : Mallocator;
import std.experimental.allocator : makeArray, dispose;
alias alloc = Mallocator.instance;
auto p = alloc.makeArray!ubyte(20);
alloc.dispose(p);
}
---
https://run.dlang.io/is/t2IMq2
No ready why this shouldn't work.
Comment #1 by ag0aep6g — 2018-02-01T15:38:52Z
How do you suppose `dispose` can be safe here? Mallocator doesn't do reference counting, so `dispose` doesn't detect any existing references except the one you pass.
In code:
----
alias alloc = Mallocator.instance;
auto p1 = alloc.makeArray!ubyte(20);
auto p2 = p1; /* Another reference to the same data. */
alloc.dispose(p); /* Nulling p1 but not p2. */
ubyte x = p2[0]; /* Dereferencing a dangling pointer. */
----
Comment #2 by greensunny12 — 2018-02-01T17:26:07Z
Ah sorry I was confusing things - I was wanted to refer to ScopedAllocator, but that already works:
---
void main() @safe
{
import std.experimental.allocator.mallocator : Mallocator;
import std.experimental.allocator.building_blocks.scoped_allocator : ScopedAllocator;
import std.experimental.allocator : makeArray, dispose;
alias alloc = Mallocator.instance;
auto p = alloc.makeArray!ubyte(20);
}
---
https://run.dlang.io/is/xJBFaA
Sorry