This code “array.remove(array.indexOf(item))” crashes because indexOf can returns -1 that remove accepts.
I know there is removeValue, but the above code can be still be required if you need to do something on the object before removing.
Well the documentation says
[quote]template<class ObjectClass, class TypeOfCriticalSectionToUse = DummyCriticalSection>
int OwnedArray< ObjectClass, TypeOfCriticalSectionToUse >::indexOf ( const ObjectClass *const objectToLookFor ) const throw ()
Finds the index of an object which might be in the array.
Parameters:
objectToLookFor the object to look for
Returns:
the index at which the object was found, or -1 if it’s not found
[/quote]
http://www.rawmaterialsoftware.com/api/classOwnedArray.html#8f0339a4d2b8213edbaa436096fab4b9
There has to be an return value check.
int index = array.indexOf(item);
if (index >= 0)
array.remove()
It crashes?? If you pass an invalid index to remove(), it just ignores it - it doesn’t crash!
Yes, my fault, didn’t see the cast to unsigned int in the range test.
The crash was due to the “return ElementType()” in the else cause which, in my case was an invalid pointer.