# Destructor needed for OwnedArray?

**URL:** <https://forum.juce.com/t/destructor-needed-for-ownedarray/28096>\
**Category:** General JUCE discussion\
**Created:** [June 12, 2018, 8:09pm UTC](https://forum.juce.com/t/destructor-needed-for-ownedarray/28096 "2018-06-12T20:09:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chaz13](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@chaz13](https://forum.juce.com/u/chaz13)\
**Post date:** [June 12, 2018, 8:09pm UTC](https://forum.juce.com/t/destructor-needed-for-ownedarray/28096/1 "2018-06-12T20:09:09Z")

</div>

Hi!

I have a question about Owned Arrays. If I have a class, like so:

```
class MyClass
{
private:
	juce::OwnedArray<SomeObject> List;
}

```

Where the OwnedArray List contains a bunch of other objects - do I need to specifically clear the OwnedArray in the destructor of MyClass, to delete the objects in List, or is this handled automatically if an instance of MyClass is deleted?

I tried to dig around a bit in the source but got a little lost. I’m still a bit new to C++!

Thanks.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [June 12, 2018, 8:12pm UTC](https://forum.juce.com/t/destructor-needed-for-ownedarray/28096/2 "2018-06-12T20:12:04Z")

</div>

That is the purpose of the OwnedArray. When the array goes out of scope, all elements are properly destroyed.  
If you get conflicts, note the order of your members, the destruction of each member of a class takes place in reverse order to the creation.

---

<div class="post-metadata">

**Author:** ![chaz13](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@chaz13](https://forum.juce.com/u/chaz13)\
**Post date:** [June 12, 2018, 8:25pm UTC](https://forum.juce.com/t/destructor-needed-for-ownedarray/28096/3 "2018-06-12T20:25:01Z")

</div>

Ah ok - so if I understand correctly:

Whenever the OwnedArray is destroyed, it’ll automatically destroy all the objects it contains, too.

And when MyClass is destroyed, the OwnedArray will be destroyed (then this being destroyed will destroy the objects it contains as noted above).

Thank you for your help 🙂

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [June 12, 2018, 8:35pm UTC](https://forum.juce.com/t/destructor-needed-for-ownedarray/28096/4 "2018-06-12T20:35:49Z")

</div>

You’d be wise to add a breakpoint in your class’s destructor and click the “step into” button in your debugger so you gain some understanding of the order that things are destructed in.

if your class doesn’t have a destructor, then create one like this and add a DBG statement:

```auto
MyClass::~MyClass()
{
    DBG( "destructing MyClass" ); //add the breakpoint on this line
}

```
