# OwnedArray and pointer safety

**URL:** <https://forum.juce.com/t/ownedarray-and-pointer-safety/41028>\
**Category:** General JUCE discussion\
**Created:** [August 1, 2020, 3:52am UTC](https://forum.juce.com/t/ownedarray-and-pointer-safety/41028 "2020-08-01T03:52:25Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jnicol](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jnicol/32/1532_2.png) [@jnicol](https://forum.juce.com/u/jnicol)\
**Post date:** [August 1, 2020, 3:52am UTC](https://forum.juce.com/t/ownedarray-and-pointer-safety/41028/1 "2020-08-01T03:52:25Z")

</div>

My grasp of pointers is quite weak, and I’m hoping someone more knowledgeable can tell me if I am using OwnedArray in a safe way.

What I’m doing is adding components (in this case Label) to an OwnedArray to make it easy to iterate over them later on. So I create each Label, add it to an OwnedArray, and do some setup on it. Here’s a stripped down example:

```auto
OwnedArray<Label> myOwnedArray;
Label* myLabel = new Label("label-name", "Some Text");
myOwnedArray.add(myLabel);
// Do some setup on myLabel, e.g.
addAndMakeVisible(myLabel);

```

I’ve been reading about how raw pointers and “new” should be avoided in favour of smart pointers, and it’s made me worried that my approach is not ideal. I _think_ my approach is safe, since OwnedArray takes ownership of the raw `myLabel` pointer?

Nevertheless I have also tried a different approach, passing a smart pointer to the OwnedArray’s add method:

```auto
OwnedArray<Label> myOwnedArray;
auto myLabel = myOwnedArray.add(std::make_unique<Label>("label-name", "Some Text"));
// Do some setup on myLabel, e.g.
addAndMakeVisible(myLabel);

```

Is one approach preferable to the other? Or is there a better approach altogether?

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [August 1, 2020, 9:03am UTC](https://forum.juce.com/t/ownedarray-and-pointer-safety/41028/2 "2020-08-01T09:03:22Z")

</div>

In your use case it doesn’t really matter if you use new or make\_unique. (Ignoring here any possible C++ exceptions stuff but you are not catching the exceptions anyway in your current code.)

---

<div class="post-metadata">

**Author:** ![jnicol](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jnicol/32/1532_2.png) [@jnicol](https://forum.juce.com/u/jnicol)\
**Post date:** [August 1, 2020, 11:11am UTC](https://forum.juce.com/t/ownedarray-and-pointer-safety/41028/3 "2020-08-01T11:11:21Z")

</div>

Thanks. I’m glad to hear that I’m not doing it completely wrong!

---

<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:** [August 1, 2020, 12:07pm UTC](https://forum.juce.com/t/ownedarray-and-pointer-safety/41028/4 "2020-08-01T12:07:16Z")

</div>

The code you’ve written looks fine, but if you’re new to smart pointers, it’s probably worth thinking about some of the reasons why you should get in the habit of using `std::make_unique`, rather than `new`.

Your code allows you to do something silly like `return myLabel`. (You don’t intend to do this right now of course, but will you remember not to do it when you make changes to your code in 6 months time?) If you did return the pointer, then you could end up giving it to some other object that has no idea about the lifetime of the object. There would then be nothing stopping it from trying to dereference a deleted object and crashing. On the other hand, if you were to return a smart pointer to the object, its lifetime would be automatically managed.

Secondly, suppose that an error is thrown after you create the `new` object but before you’ve handed it to the `OwnedArray`. (Again, it’s not going to happen in the code you’ve written, but suppose you come back in a year and add the line of code `myLabel->doSomethingRisky()` on the third line). If an error is thrown, the stack will unwind, and when it passes the current scope, you’ll be left with a memory leak. But this can’t happen if you’re using `std::make_unique`, as the destructor will automatically delete the object.

Personally, I allow myself to use `new` _only_ when I’m placing it directly into the (smart) container, so that I know it can’t be exposed.

```
 myOwnedArray.add(new Label("label-name", "Some Text"));

```

All other times, I try to make sure that I’m using smart pointers.

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [August 1, 2020, 12:23pm UTC](https://forum.juce.com/t/ownedarray-and-pointer-safety/41028/5 "2020-08-01T12:23:29Z")

</div>

Regarding exceptions, I would mostly ignore anything to do with them in Juce based code. They can only be sensibly dealt with in Juce stand alone applications by setting the exception handler function but in plugins, as far as I know, you would need to add try catch blocks everywhere where an exception might happen. I have myself usually opted for “if a C++ exception happens, just let the code crash” but if there actually _is_ a way to add a centralized exception handler for plugins too, I’d be interested to know how that can be done…

---

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [August 1, 2020, 4:31pm UTC](https://forum.juce.com/t/ownedarray-and-pointer-safety/41028/6 "2020-08-01T16:31:50Z")

</div>

> [@xenakios](#):
>
> Regarding exceptions, I would mostly ignore anything to do with them in Juce based code.

yes, but what @LiamG suggest is just good practice. or, said another way, bad practice in places where it doesn’t matter encourages bad practices… 😉

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [August 1, 2020, 5:57pm UTC](https://forum.juce.com/t/ownedarray-and-pointer-safety/41028/7 "2020-08-01T17:57:00Z")

</div>

> [@jnicol](#):
>
> ```auto
> OwnedArray<Label> myOwnedArray;
> auto myLabel = myOwnedArray.add(std::make_unique<Label>("label-name", "Some Text"));
> // Do some setup on myLabel, e.g.
> addAndMakeVisible(myLabel);
> 
> ```

If it makes sense in the context of your application I’d move the ownership transfer at the end like that

```auto
// First create it
auto myLabel = std::make_unique<Label> ("label-name", "Some Text");

// Work on it
addAndMakeVisible (*myLabel);
myLabel->setSomething();

// Transfer ownership
myOwnedArray.add (std::move (myLabel));

// Note: Down here myLabel is no longer valid as it has been moved

```

When code gets more complex, this e.g. allows you to safely do something like that:

```auto
auto myLabel = std::make_unique<Label> ("label-name", "Some Text");

// some block of code working with myLabel

if (someCondition)
    return; // myLabel will safely be deleted here

myOwnedArray.add (std::move (myLabel));

```

---

<div class="post-metadata">

**Author:** ![jnicol](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jnicol/32/1532_2.png) [@jnicol](https://forum.juce.com/u/jnicol)\
**Post date:** [August 1, 2020, 8:23pm UTC](https://forum.juce.com/t/ownedarray-and-pointer-safety/41028/8 "2020-08-01T20:23:14Z")

</div>

> [@LiamG](#):
>
> The code you’ve written looks fine, but if you’re new to smart pointers, it’s probably worth thinking about some of the reasons why you should get in the habit of using `std::make_unique` , rather than `new` .

The reasons you’ve listed are enough to convince me that smart pointers are the better option. Thanks for spelling out the benefits 🙂

---

<div class="post-metadata">

**Author:** ![jnicol](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jnicol/32/1532_2.png) [@jnicol](https://forum.juce.com/u/jnicol)\
**Post date:** [August 1, 2020, 8:31pm UTC](https://forum.juce.com/t/ownedarray-and-pointer-safety/41028/9 "2020-08-01T20:31:27Z")

</div>

> [@PluginPenguin](#):
>
> ```auto
> // Transfer ownership
> myOwnedArray.add (std::move (myLabel));
> 
> ```

I was actually struggling to figure out how to create the smart pointer outside of `add()`, then add it to the OwnedArray later on. `std::move` works great!

---

<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:** [August 2, 2020, 4:04am UTC](https://forum.juce.com/t/ownedarray-and-pointer-safety/41028/10 "2020-08-02T04:04:01Z")

</div>

> [@jnicol](#):
>
> I was actually struggling to figure out how to create the smart pointer outside of `add()` , then add it to the OwnedArray later on. `std::move` works great!

Another option for this is `myOwnedArray.add(myLabel.release())`. The `std::unique_ptr::release()` function returns the raw pointer and then nullifies the `unique_ptr`. (If you look inside the `OwnedArray::add(std::unique_ptr)` function you’ll see that it’s only calling `release()` anyway). You just need to make sure that you don’t call anything on the `unique_ptr` after `release()`, or else you’ll get a runtime error.

Here is another option which avoids having any exposed pointers, smart or raw:

```
OwnedArray<Label> myOwnedArray;
myOwnedArray.add(new Label("label-name", "Some Text"));
addAndMakeVisible(myOwnedArray.getLast());

```

---

<div class="post-metadata">

**Author:** ![jnicol](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jnicol/32/1532_2.png) [@jnicol](https://forum.juce.com/u/jnicol)\
**Post date:** [August 2, 2020, 11:52am UTC](https://forum.juce.com/t/ownedarray-and-pointer-safety/41028/11 "2020-08-02T11:52:06Z")

</div>

> [@LiamG](#):
>
> Another option for this is `myOwnedArray.add(myLabel.release())`

Good to know. I’m learning a lot about smart pointers today!

---

<div class="post-metadata">

**Author:** ![connorreviere](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/connorreviere/32/4890_2.png) [@connorreviere](https://forum.juce.com/u/connorreviere)\
**Post date:** [August 3, 2020, 4:24pm UTC](https://forum.juce.com/t/ownedarray-and-pointer-safety/41028/12 "2020-08-03T16:24:41Z")

</div>

You can use `make_unique` to avoid using `new` there, and since `OwnedArray::add` returns a pointer to the element that was added you can just do:

```
OwnedArray<Label> myOwnedArray;
addAndMakeVisible (myOwnedArray.add (std::make_unique<Label> ("Name", "Text")));
```
