Bug 21775 – std.typecons.RefCounted, std.container.array.Array, & similar structs that manage their own memory do not need to be scanned unless GC-allocated memory is reachable through them
Comment #0 by n8sh.secondary — 2021-03-28T02:27:37Z
Code that manually allocates memory without using the GC is responsible for calling GC.addRange if there is the possibility the memory might contain a pointer through which GC-allocated memory is reachable. Conservatively determining this via std.traits.hasIndirections can lead to false positives when nesting multiple types that manage their own memory, for instance a refcounted array of refcounted items.
There is a related problem in the Phobos implementation of std.container.array.Array that affects every Array regardless of its element type. Array!E is shaped like:
---
import std.typecons : RefCounted;
struct Array(E)
{
static struct Payload
{
size_t capacity;
E[] payload; // allocated with malloc.
/// ...
}
auto data = RefCounted!Payload;
/// ...
}
---
Because Payload has a pointer to an array, RefCounted!(Array!E.Payload) calls GC.addRange irrespective of E.
This situation can be improved. Rather than use a general-purpose check for whether a type has pointers, RefCounted and related types can use a special-purpose slightly altered version that need not be exposed to library users.
Comment #1 by dlang-bot — 2021-03-28T02:33:35Z
@n8sh created dlang/phobos pull request #7923 "std.typecons.RefCounted, std.container.array.Array, & similar structs that manage their own memory do not need to be scanned unless GC-allocated memory is reachable through them" mentioning this issue:
- Issue 21775 - std.typecons.RefCounted, std.container.array.Array, & similar structs that manage their own memory do not need to be scanned unless GC-allocated memory is reachable through them
https://github.com/dlang/phobos/pull/7923
Comment #2 by robert.schadek — 2024-12-01T16:38:37Z