Concept

# Destructor

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

A special method that is called automatically when an object goes out of scope.

## Usage

``` xojo
Destructor 
```

## Notes

The <span class="title-ref">Destructor</span> method is called automatically, so you would never call it yourself. It takes no parameters and does not return a value.

Destructors are typically used to do any "clean up" that is not done for you automatically. If it exists, a class's <span class="title-ref">Destructor</span> is called automatically when an instance of the class is deleted or goes out of scope — for example, when the user closes the window.

To create the <span class="title-ref">Destructor</span> for a custom class, add a method to the class and name it "<span class="title-ref">Destructor</span>".

Destructors are called when the last reference to an object is removed, even if execution is in a <span class="title-ref">Destructor</span> for another object. Note that this means you can cause a stack overflow if your <span class="title-ref">Destructor</span> triggers other destructors in a deep recursion. However, such overflow will not happen as long as properties of the object are being cleaned up automatically. So, it is generally preferable to not set properties to `Nil</api/language/nil>` in your <span class="title-ref">Destructor</span>, but instead let them be cleaned up for you.

Unlike a `Constructor</api/language/constructor>` or other methods, the <span class="title-ref">Destructor</span> in a subclass does not override its superclass <span class="title-ref">Destructor</span>. Normally you would not call `Super</api/language/super>`.Destructor in your subclass' <span class="title-ref">Destructor</span> since this will cause the superclass' <span class="title-ref">Destructor</span> to fire twice.

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`Destructors Can't Have Parameters</api/errors>` error; `Constructor</api/language/constructor>`, `Constructors and Destructors</getting_started/object-oriented_programming/constructors_and_destructors>` topics

</div>
