Calling destroy on struct pointer

Moritz Maxeiner via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Feb 25 08:39:18 PST 2017


On Saturday, 25 February 2017 at 15:13:27 UTC, Radu wrote:
>
> Here is sample on how destroy fails with a fwd decl error:
>
> struct A
> {
>     B b;
>     C c;
> }
>
> struct B
> {
>     Wrap!A val;
> }
>
> struct C
> {
>     Wrap!A val;
> }
>
> struct Wrap(T)
> {
>
>     this(bool b)
>     {
>         t = cast(T*) malloc(T.sizeof);
>     }
>
>     ~this()
>     {
>         destroy(*t);  // Error: struct app.A no size because of 
> forward reference
>     }
>     T* t;
> }

Thanks for the example.

>
> Manual management fails now with the current construct, inst't 
> it?

Hm, that's an issue you'd best take up to the bugtracker, I 
think. Maybe there's a way around that, but I don't know.

>
> auto a = cast (A*) malloc(A.sizeof); // Allocate
> emplace(a, 42);                      // Construct
>
> destroy(a);                          // Destruct
> |-- here a becomes null
> assert(a is null); // :}
> free(a);                             // Deallocate
> |- free null...
>
> You need to save a into a temp, then call free on temp.
>
> A nice to have enhancement would be to return the destroyed 
> pointer from destroy, enabling something like:
>
> destroy(a).free();

Well, yes, but that is still backwards-incompatible and breaking 
user code is something I was under the impression was a big NO 
currently.


More information about the Digitalmars-d-learn mailing list