# OwnedArray and SLT's remove function

**URL:** <https://forum.juce.com/t/ownedarray-and-slts-remove-function/18744>\
**Category:** General JUCE discussion\
**Created:** [August 16, 2016, 1:53pm UTC](https://forum.juce.com/t/ownedarray-and-slts-remove-function/18744 "2016-08-16T13:53:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![widdershins](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/widdershins/32/20_2.png) [@widdershins](https://forum.juce.com/u/widdershins)\
**Post date:** [August 16, 2016, 1:53pm UTC](https://forum.juce.com/t/ownedarray-and-slts-remove-function/18744/1 "2016-08-16T13:53:34Z")

</div>

I’m trying to increase my usage of STL algorithms to avoid reinventing wheels. I use JUCE’s OwnedArray class in certain places, but I’ve found that the .end() and .begin() methods in Array and OwnedArray make using STL algorithms quite seamless.

Now I’ve run into a problem. I have an OwnedArray where I want to remove elements that match a certain criteria. This is a perfect fit for std::remove\_if, which lets you use a lambda predicate, very convenient. After remove\_if (which just moves elements which match the predicate to the end and returns an iterator to the point where the matched elements begin) the normal STL thing would be to call erase() on the container. I have replaced this with a call to ownedArray.removeLast (ownedArray.end() - firstMatchedElementIterator).

This appears to work fine, but later when I call a virtual method on one of the remaining elements (it’s a polymorphous list of objects) I get a bad access error. Is the STL algorithm moving pointer elements somehow causing the program to think the object is the wrong type? Or is there something wrong with the pointer arithmetic I’m doing on the iterators?

---

<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:** [August 16, 2016, 2:05pm UTC](https://forum.juce.com/t/ownedarray-and-slts-remove-function/18744/2 "2016-08-16T14:05:13Z")

</div>

Is it because the value returned by begin() becomes invalid when you delete an item?

---

<div class="post-metadata">

**Author:** ![widdershins](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/widdershins/32/20_2.png) [@widdershins](https://forum.juce.com/u/widdershins)\
**Post date:** [August 16, 2016, 2:08pm UTC](https://forum.juce.com/t/ownedarray-and-slts-remove-function/18744/3 "2016-08-16T14:08:34Z")

</div>

I’m not using the .begin() iterator to access afterwards, it’s in a separate function which basically does:

```
if (!ownedArray.empty())
    ownedArray[0]->virtualFunction()
```

---

<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:** [August 16, 2016, 2:13pm UTC](https://forum.juce.com/t/ownedarray-and-slts-remove-function/18744/4 "2016-08-16T14:13:23Z")

</div>

Ah, don’t know then. Would need to see all the code to figure that out.

---

<div class="post-metadata">

**Author:** ![widdershins](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/widdershins/32/20_2.png) [@widdershins](https://forum.juce.com/u/widdershins)\
**Post date:** [August 16, 2016, 2:16pm UTC](https://forum.juce.com/t/ownedarray-and-slts-remove-function/18744/5 "2016-08-16T14:16:56Z")

</div>

Yeah, perhaps I should try a std::vector of unique\_ptrs to see if it’s the JUCE containers or (more likely) something else in my code that’s causing the problem…

---

<div class="post-metadata">

**Author:** ![widdershins](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/widdershins/32/20_2.png) [@widdershins](https://forum.juce.com/u/widdershins)\
**Post date:** [August 16, 2016, 2:40pm UTC](https://forum.juce.com/t/ownedarray-and-slts-remove-function/18744/6 "2016-08-16T14:40:17Z")

</div>

Well rather than switch to a std::vector of unique\_ptrs, which would have required minor changes in lots of places, I just rolled my own ugly version of remove\_if:

```
Array<MyBaseClass*> toRemove;
        
for (int i = 0; i != uGenViews.size(); ++i)
    if (ownedArray[i]->match()))
        toRemove.add (ownedArray[i]);
        
for (int i = 0; i != toRemove.size(); ++i)
    ownedArray.removeObject (toRemove[i]);

```

Which solves the problem. This was the kind of code I was trying to avoid writing, and it’s certainly going to be less efficient, though that doesn’t really matter in this case, the OwnedArray will rarely have more than 20 or so items, and this isn’t hot code.
