Finalize GC memory

Jonathan M Davis jmdavisProg at gmx.com
Mon Jun 17 09:53:59 PDT 2013


On Monday, June 17, 2013 18:46:29 Namespace wrote:
> On Sunday, 16 June 2013 at 21:37:16 UTC, Namespace wrote:
> > It seems that does what I want. The result is the same as with
> > the current 'delete' implementation.
> > 
> > ----
> > void Delete(T)(ref T var) {
> > 
> > static if (is(T == struct) && is(typeof(var.__dtor)))
> > 
> > var.__dtor();
> > 
> > static if (is(T : U[], U))
> > 
> > core.memory.GC.free(var.ptr);
> > 
> > else {
> > 
> > static if (is(T : U*, U))
> > 
> > core.memory.GC.free(var);
> > 
> > else
> > 
> > core.memory.GC.free(&var);
> > 
> > }
> > 
> > }
> > ----
> > 
> > But if I call 'destroy' before I call GC.free, it does not work
> > correct.
> 
> I forget 'var = null;'.
> 
> But:
> Any thoughts on this?
> Will it work as I want?
> In my few tests it seems to work like delete, but I would be
> interested in the meaning of some experts.

I'd be worried about whether it actually called the destructors of the members 
of the struct. IIRC, you actually have to play around with TypeInfo in order 
to be able to correctly manually destroy a struct rather than simply calling 
its __dtor method, but it's not something that I ever do, so I don't remember 
the details. Regardless, if destroy doesn't actually destroy a struct 
correctly, then that's a bug and should be reported.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list