www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 14402] New: std.conv.emplace for classes segfaults for nested


          Issue ID: 14402
           Summary: std.conv.emplace for classes segfaults for nested
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: Phobos
          Assignee: nobody puremagic.com
          Reporter: mkline.on.d 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

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);

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

    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[])
    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

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

Apr 03 2015