View source code
Display the source code in object.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function object.destroy

Destroys the given object and sets it back to its initial state. It's used to destroy an object, calling its destructor or finalizer so it no longer references any other objects. It does not initiate a GC cycle or free any GC memory.

void destroy(T) (
  T obj
)
if (is(T == class));

void destroy(T) (
  T obj
)
if (is(T == interface));

void destroy(T) (
  ref T obj
)
if (is(T == struct));

void destroy(T, U, ulong n) (
  ref T obj
)
if (!is(T == struct));

void destroy(T) (
  ref T obj
)
if (!is(T == struct) && !is(T == interface) && !is(T == class) && !_isStaticArray!T);

Example

Reference type demonstration

class C
{
    static int dtorCount;

    string s = "S";
    ~this() { dtorCount++; }
}

C c = new C();
assert(c.dtorCount == 0); // destructor not yet called
assert(c.s == "S");       // initial state `c.s` is `"S"`
c.s = "T";
assert(c.s == "T");       // `c.s` is `"T"`
destroy(c);
assert(c.dtorCount == 1); // `c`'s destructor was called
assert(c.s == "S");       // `c.s` is back to its inital state, `"S"`

Example

Value type demonstration

int i;
assert(i == 0);           // `i`'s initial state is `0`
i = 1;
assert(i == 1);           // `i` changed to `1`
destroy(i);
assert(i == 0);           // `i` is back to its initial state `0`

Authors

Walter Bright, Sean Kelly

License

Boost License 1.0.