Variables with scoped destruction in closures
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Oct 14 10:55:18 PDT 2016
On 10/14/16 6:18 AM, Nordlöw wrote:
> The following code
>
>
> import std.algorithm.iteration : filter;
> import std.algorithm.mutation : move;
> import std.range : iota;
>
> static private struct S
> {
> import core.memory : GC;
> @disable this(this);
>
> this(int x)
> {
> _ptr = cast(typeof(_ptr))GC.malloc((*_ptr).sizeof);
> *_ptr = x;
> }
>
> ~this() { GC.free(_ptr); } // scoped destruction
Note: no matter what solution you can come up with, having the GC
destroy this struct, and the destructor attempting to free the pointer,
is going to result in errors.
Instead, you should use C malloc/free here.
This is what RefCounted does.
-Steve
More information about the Digitalmars-d-learn
mailing list