www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Destructing Struct

reply Jiyan <jiyan jiyan.info> writes:
Hi :),

What i thought was that when i create a struct dynamically i can 
just deconstruct it with __dtor lets say:

struct U {...}
struct S {... private U _member;}

S* p;
p = cast(S*)malloc(S.sizeof);

// just run that if it compiles, for simplicity
// we dont use __traits(compiles, ...)
p.__dtor;

The thing here is that this doesn't work because of when S has an 
element that that is private and has a __dtor itself, the __dtor 
from U doesnt get called before the call of __dtor from S - or 
after.

Is there any way with traits or sth to do that?

Are delete, destroy or any other functions the standard library 
working here?
I would prefer a solution that can be build by myself - so 
without the standard library for example with traits.

Thanks :)
Feb 21 2018
next sibling parent reply Jiyan <jiyan jiyan.info> writes:
On Wednesday, 21 February 2018 at 11:12:01 UTC, Jiyan wrote:
 Hi :),

 What i thought was that when i create a struct dynamically i 
 can just deconstruct it with __dtor lets say:

 struct U {...}
 struct S {... private U _member;}

 S* p;
 p = cast(S*)malloc(S.sizeof);

 // just run that if it compiles, for simplicity
 // we dont use __traits(compiles, ...)
 p.__dtor;

 The thing here is that this doesn't work because of when S has 
 an element that that is private and has a __dtor itself, the 
 __dtor from U doesnt get called before the call of __dtor from 
 S - or after.

 Is there any way with traits or sth to do that?

 Are delete, destroy or any other functions the standard library 
 working here?
 I would prefer a solution that can be build by myself - so 
 without the standard library for example with traits.

 Thanks :)
I think i found my solution: is it __xdtor? :P
Feb 21 2018
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 2/21/18 6:24 AM, Jiyan wrote:
 I think i found my solution: is it __xdtor? :P
Yes, that is the function that will run *recursively* all the destructors (just __dtor runs the destructor method if you provided one). But I'd recommend as the others did, using destroy. -Steve
Feb 21 2018
prev sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
Jiyan wrote:

 Hi :),

 What i thought was that when i create a struct dynamically i can just 
 deconstruct it with __dtor lets say:

 struct U {...}
 struct S {... private U _member;}

 S* p;
 p = cast(S*)malloc(S.sizeof);

 // just run that if it compiles, for simplicity
 // we dont use __traits(compiles, ...)
 p.__dtor;

 The thing here is that this doesn't work because of when S has an element 
 that that is private and has a __dtor itself, the __dtor from U doesnt 
 get called before the call of __dtor from S - or after.

 Is there any way with traits or sth to do that?

 Are delete, destroy or any other functions the standard library working 
 here?
 I would prefer a solution that can be build by myself - so without the 
 standard library for example with traits.

 Thanks :)
`p.destroy` will call the dtors for you. you'd better not use `__`-frefixed symbols yourself, as they aren't actually a part of a language, they're just implementation details.
Feb 21 2018
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 21 February 2018 at 12:07:47 UTC, ketmar wrote:
 `p.destroy` will call the dtors for you.
So it is the same function but I prefer to always write it: .destroy(p); yes, a leading dot. This ensures you call the top-level destroy function instead of any members which may not do the same thing.
Feb 21 2018