[Issue 14402] New: std.conv.emplace for classes segfaults for nested class

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri Apr 3 01:27:09 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=14402

          Issue ID: 14402
           Summary: std.conv.emplace for classes segfaults for nested
                    class
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: Phobos
          Assignee: nobody at puremagic.com
          Reporter: mkline.on.d at gmail.com

I came across this while doing work on std.typecons.Unique (see
https://github.com/D-Programming-Language/phobos/pull/3139). Any class that
accesses a local context seems to segfault when emplaced. A minimal test case
follows:

import core.stdc.stdlib : malloc, free;
import std.conv : emplace;
import std.traits : classInstanceAlignment;

void main()
{
    int created;
    int destroyed;
    class Foo {
        this() { ++created; }
        ~this() { ++destroyed; }
    }

    immutable size_t size = __traits(classInstanceSize, Foo);

    void* m = malloc(size);
    assert(m);

    Foo f = emplace!Foo(m[0 .. size]);
    assert(created == 1);

    f.destroy();
    free(m);
    assert(destroyed == 1);
}

The problem doesn't seem to be the amount of memory I am allocating, given that
it makes it past the testEmplaceChunk call in emplace. A stack trace from GDB
is as follows:

Program received signal SIGSEGV, Segmentation fault.
0x000000000042afd4 in wat.main().Foo.this() (this=0x66d450) at wat.d:10
10            this() { ++created; }
(gdb) where
#0  0x000000000042afd4 in wat.main().Foo.this() (this=0x66d450) at wat.d:10
#1  0x000000000042b167 in std.conv.emplace!(wat.main().Foo).emplace(void[])
(chunk=...)
    at /home/mrkline/src/dlang/phobos/std/conv.d:5005
#2  0x000000000042af76 in D main () at wat.d:19

where the relevant line in conv.d is
result.__ctor(args);

This was seen on 2.067 and the current master for dmd and phobos as of
2015-04-03 01:20 PST.

--


More information about the Digitalmars-d-bugs mailing list