When I should to call destroy?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 29 06:56:33 PDT 2016


On Friday, July 29, 2016 13:18:00 Suliman via Digitalmars-d-learn wrote:
> Use the `destroy` function to finalize an object by calling its
> destructor. The memory of the object is not immediately
> deallocated, instead the GC will collect the memory of the object
> at an undetermined point after finalization:
>
> class Foo { int x; this() { x = 1; } }
> Foo foo = new Foo;
> destroy(foo);
> assert(foo.x == int.init);  // object is still accessible
>
>
> But I can't understand if D have GC it should remove objects when
> their life is finished. When I should to call `destroy`? What
> would be if I will not call it?

destroy is for when you need to explicitly call the destructor on an object.
It destroys it without freeing the memory. There are some rare cases where
that is extremely useful, but odds are, you'll never need it.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list