# Is OwnedArray::addArray() unsafe?

**URL:** <https://forum.juce.com/t/is-ownedarray-addarray-unsafe/39142>\
**Category:** General JUCE discussion\
**Created:** [May 2, 2020, 8:22am UTC](https://forum.juce.com/t/is-ownedarray-addarray-unsafe/39142 "2020-05-02T08:22:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![LiamG](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/liamg/32/6844_2.png) [@LiamG](https://forum.juce.com/u/LiamG)\
**Post date:** [May 2, 2020, 8:22am UTC](https://forum.juce.com/t/is-ownedarray-addarray-unsafe/39142/1 "2020-05-02T08:22:40Z")

</div>

I ran into a simple RAII problem with `OwnedArray`, where I was trying to move objects from one Array to another, but ended up deleting them twice. This got me thinking about the `addArray()` method on `OwnedArray`–isn’t it fundamentally unsafe?

```
{
    OwnedArray<Component> a, b;

    a.add(new Component());

    b.addArray(a); // Component* is now owned by both a and b
}
// scope ends, Component* is deleted by a and b. Crash!

```

Is there a possible use case where this method isn’t unsafe? If not, should it be allowed to compile?

---

<div class="post-metadata">

**Author:** ![kunitoki](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kunitoki/32/15_2.png) [@kunitoki](https://forum.juce.com/u/kunitoki)\
**Post date:** [May 2, 2020, 5:00pm UTC](https://forum.juce.com/t/is-ownedarray-addarray-unsafe/39142/2 "2020-05-02T17:00:30Z")

</div>

`std::vector<std::unique_ptr<Component>>` is simply a better choice nowadays. it’s explicit and avoids shooting yourself in the foot.
