Destructor needed for OwnedArray?


#1

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.


#2

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.


#3

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:


#4

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:

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