# Beginner question regarding polymorphism and OwnedArray leaks

**URL:** <https://forum.juce.com/t/beginner-question-regarding-polymorphism-and-ownedarray-leaks/16388>\
**Category:** General JUCE discussion\
**Created:** [January 13, 2016, 6:14am UTC](https://forum.juce.com/t/beginner-question-regarding-polymorphism-and-ownedarray-leaks/16388 "2016-01-13T06:14:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Chapman](https://avatars.discourse-cdn.com/v4/letter/m/6bbea6/32.png) [@Mike\_Chapman](https://forum.juce.com/u/Mike_Chapman)\
**Post date:** [January 13, 2016, 6:14am UTC](https://forum.juce.com/t/beginner-question-regarding-polymorphism-and-ownedarray-leaks/16388/1 "2016-01-13T06:14:22Z")

</div>

As a beginner I am loving the framework so far and everything has fallen into place - thanks Jules and co.

I am having some trouble with using inheritance and the OwnedArray and know I am missing something simple. I have tried to condense the problem down into its simplest form:

```
class Base
{
};

class Derived : public Base
{
    OwnedArray<Base> list;
};

class Test
{
public:
    OwnedArray<Base> stuff;
};

Test t;
t.stuff.add(new Derived());
```

When I run this it leaks ie.

\*\*\* Leaked objects detected: 1 instance(s) of class OwnedArray

If I move 'OwnedArray\<Base\> list' into Base then no leak occurs.

I am not sure where my mistake is and am a bit stuck as I need inheritance for these classes.&nbsp;&nbsp;

Any help would be gladly appreciated.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [January 13, 2016, 8:56am UTC](https://forum.juce.com/t/beginner-question-regarding-polymorphism-and-ownedarray-leaks/16388/2 "2016-01-13T08:56:10Z")

</div>

Base needs a virtual destructor.

---

<div class="post-metadata">

**Author:** ![Mike\_Chapman](https://avatars.discourse-cdn.com/v4/letter/m/6bbea6/32.png) [@Mike\_Chapman](https://forum.juce.com/u/Mike_Chapman)\
**Post date:** [January 13, 2016, 9:42am UTC](https://forum.juce.com/t/beginner-question-regarding-polymorphism-and-ownedarray-leaks/16388/3 "2016-01-13T09:42:26Z")

</div>

Fixed. Thank you so much!

C++ has been a struggle for me so far, but using Juce has been a pleasure. You have created a great piece of software.

&nbsp;

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [January 13, 2016, 9:45am UTC](https://forum.juce.com/t/beginner-question-regarding-polymorphism-and-ownedarray-leaks/16388/4 "2016-01-13T09:45:35Z")

</div>

:)
