Skip to content

std.allocator: Regions are non-copyable, yet are passed around in examples #10334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dlangBugzillaToGithub opened this issue May 10, 2018 · 1 comment · May be fixed by #6509
Open

std.allocator: Regions are non-copyable, yet are passed around in examples #10334

dlangBugzillaToGithub opened this issue May 10, 2018 · 1 comment · May be fixed by #6509

Comments

@dlangBugzillaToGithub
Copy link

dlang-bugzilla (@CyberShadow) reported this on 2018-05-10T11:21:07Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=18848

CC List

Description

Here is an example from std.experimental.allocator.building_blocks.allocator_list.AllocatorList:

// Ouroboros allocator list based upon 4MB regions, fetched directly from
// mmap. All memory is released upon destruction.
alias A1 = AllocatorList!((n) => Region!MmapAllocator(max(n, 1024 * 4096)),
    NullAllocator);

The example features a lambda returning a Region.

However, Regions cannot be copied, as they have a destructor which deallocates everything. A comment in the implementation says:

    /*
    TODO: The postblit of $(D BasicRegion) should be disabled because such objects
    should not be copied around naively.
    */

(That's the only occurrence of BasicRegion in Phobos, so I assume that's just an old name for Region.)

Though NRVO may make the verbatim example code behave correctly, it is fragile, and small code modifications e.g. to work around issue 18848 can cause difficult-to-debug problems (due to dangling pointers in other Region instances).
@dlangBugzillaToGithub
Copy link
Author

dlang-bugzilla (@CyberShadow) commented on 2018-05-15T23:03:19Z

https://github.com/dlang/phobos/pull/6509

See the comment there for more discussion.

@LightBender LightBender removed the P3 label Dec 6, 2024
0xEAB pushed a commit to CyberShadow/phobos that referenced this issue Mar 12, 2025
…ssed around in examples

This doesn't fix that non-copyable regions are still passed around in
examples, so we still rely on NRVO to do its thing and elide the
copy, but at least this will now catch wrong code that mistakenly
copied Regions around.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants