# OwnedArray problem size = 1

**URL:** <https://forum.juce.com/t/ownedarray-problem-size-1/422>\
**Category:** General JUCE discussion\
**Created:** [October 1, 2005, 8:48am UTC](https://forum.juce.com/t/ownedarray-problem-size-1/422 "2005-10-01T08:48:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cialz](https://avatars.discourse-cdn.com/v4/letter/c/8c91f0/32.png) [@cialz](https://forum.juce.com/u/cialz)\
**Post date:** [October 1, 2005, 8:48am UTC](https://forum.juce.com/t/ownedarray-problem-size-1/422/1 "2005-10-01T08:48:49Z")

</div>

When the cardinality of OwnedArray Object is equal to 1, oa.remove(0) will crash. :?:

Could you help me?

thanx

---

<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:** [October 1, 2005, 9:39am UTC](https://forum.juce.com/t/ownedarray-problem-size-1/422/2 "2005-10-01T09:39:10Z")

</div>

…by cardinality, you mean the size of the array?

It’ll only crash if there’s a problem deleting that object - most likely you’ve already deleted it by some other means and you’ve got a dangling pointer.

---

<div class="post-metadata">

**Author:** ![cialz](https://avatars.discourse-cdn.com/v4/letter/c/8c91f0/32.png) [@cialz](https://forum.juce.com/u/cialz)\
**Post date:** [October 1, 2005, 9:56am UTC](https://forum.juce.com/t/ownedarray-problem-size-1/422/3 "2005-10-01T09:56:33Z")

</div>

> [@](#):
>
> …by cardinality, you mean the size of the array?

Yes Jules, the size.

> [@](#):
>
> It’ll only crash if there’s a problem deleting that object

Exactly, the remove() crashes in the last line

```auto
delete toDelete;
```

`	
...
irList.insert(0,&dlgResult);
removeFilesWithoutTheseProperties(getFilter()->getNumOutputChannels(),getFilter()->getSampleRate());
...`

In removeFilesWithoutTheseProperties I do

irList.remove(i);

It is the same I do when irList has more than one objects.  
It crashes only if irList.size() = 1.

---

<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:** [October 1, 2005, 10:00am UTC](https://forum.juce.com/t/ownedarray-problem-size-1/422/4 "2005-10-01T10:00:21Z")

</div>

Looks like a basic c++ blooper to me. You’re adding a reference to something, then deleting the object, which is maybe on the stack?

---

<div class="post-metadata">

**Author:** ![cialz](https://avatars.discourse-cdn.com/v4/letter/c/8c91f0/32.png) [@cialz](https://forum.juce.com/u/cialz)\
**Post date:** [October 1, 2005, 10:04am UTC](https://forum.juce.com/t/ownedarray-problem-size-1/422/5 "2005-10-01T10:04:02Z")

</div>

When irList has more objects I use findChildFiles to fill it.

In this case I have only one result in dglResult, it is a member of the Editor class.

So you think it is better,in this case, set the 0 element to NULL instead of removing it?

---

<div class="post-metadata">

**Author:** ![cialz](https://avatars.discourse-cdn.com/v4/letter/c/8c91f0/32.png) [@cialz](https://forum.juce.com/u/cialz)\
**Post date:** [October 1, 2005, 10:16am UTC](https://forum.juce.com/t/ownedarray-problem-size-1/422/6 "2005-10-01T10:16:19Z")

</div>

Instead of  
irList.insert(0,&dlgResult);

I do  
irList.add (new File (dlgResult));

It seems to work fine now.

---

<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:** [October 1, 2005, 10:17am UTC](https://forum.juce.com/t/ownedarray-problem-size-1/422/7 "2005-10-01T10:17:16Z")

</div>

yep. Deleting objects on the stack is never a good move.

---

<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:** [May 12, 2017, 9:53am UTC](https://forum.juce.com/t/ownedarray-problem-size-1/422/8 "2017-05-12T09:53:29Z")

</div>

Closed on May 12, 2017, 9:53am UTC


