September 25, 2017
On Monday, 25 September 2017 at 08:39:26 UTC, Adrian Matoga wrote:
> [...]
>
> You shouldn't store the pointer to barBuffer inside Foo. The language allows moving the structure around with a simple memcpy, so _bar is likely to point into garbage soon after it's assigned.

Good point - but it's a mistake ;) 'Foo' is a class in the OP's code, so no problem.

> Why don't you just return *cast(Bar*)barBuffer.ptr in bar()?

Lazy construction

> You could still emplace a Bar inside barBuffer in Foo's constructor, if needed.

So you KNOW it's a class then...since structs can't have default ctors.. :P

September 28, 2017
Here's a simple solution.  Just make Bar a pointer and free it before it can be destructed!


import std.stdio;

struct Bar {
    ~this() {
        writeln("~bar");
    }
}

struct Foo {
    Bar *bar;
    this(int why_the_fuck_dont_structs_have_default_constructors) {
        bar = new Bar;
    }
    ~this() {
        writeln("~foo");
        import core.memory;
        GC.free(bar);
    }
}
September 28, 2017
Here's a simple solution.  Just make Bar a pointer and free it before it can be destructed!


import std.stdio;

struct Bar {
    ~this() {
        writeln("~bar");
    }
}

struct Foo {
    Bar *bar;
    this(int why_the_fuck_dont_structs_have_default_constructors) {
        bar = new Bar;
    }
    ~this() {
        writeln("~foo");
        import core.memory;
        GC.free(bar);
    }
}
1 2
Next ›   Last »