Destructor needed for OwnedArray?


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

class MyClass
	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++!


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.

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 :slight_smile:

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:

    DBG( "destructing MyClass" ); //add the breakpoint on this line