# Destroying an array of SplineMeshComponents

**URL:** <https://forums.unrealengine.com/t/destroying-an-array-of-splinemeshcomponents/380875>\
**Category:** C++\
**Tags:** splinemeshcomponent, tarray:-arrays-in-un, unreal-engine, crash, CPP, question\
**Created:** [January 4, 2017, 2:10pm UTC](https://forums.unrealengine.com/t/destroying-an-array-of-splinemeshcomponents/380875 "2017-01-04T14:10:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![YawLighthouse](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/yawlighthouse/32/1125831_2.png) [@YawLighthouse](https://forums.unrealengine.com/u/YawLighthouse)\
**Post date:** [January 4, 2017, 2:10pm UTC](https://forums.unrealengine.com/t/destroying-an-array-of-splinemeshcomponents/380875/1 "2017-01-04T14:10:45Z")

</div>

I’m trying to destroy all the elements of an array of spline mesh components using a for loop to destroy each mesh but every time it enters the array, the editor crashes and tells me: Array index out of bounds : 16 from array of size 16(The size of the array will be changing at runtime so I can’t create a static array). I appreciate any help on what is the proper way of doing this task. _Note_ I have tried setting index to just the array’s Num() without the -1. But this does not fix the issue either.

```
void ControllerItem::ClearArc()
{
	if (splineMeshes.Num() > 0)
	{
		for (int32 index = splineMeshes.Num() - 1; index > 0; index--)
		{
			splineMeshes[index]->DestroyComponent();
		}
		splineMeshes.Empty();
		ArcSpline->ClearSplinePoints(true);
	}	
}
```

---

<div class="post-metadata">

**Author:** ![YawLighthouse](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/yawlighthouse/32/1125831_2.png) [@YawLighthouse](https://forums.unrealengine.com/u/YawLighthouse)\
**Post date:** [January 4, 2017, 5:26pm UTC](https://forums.unrealengine.com/t/destroying-an-array-of-splinemeshcomponents/380875/2 "2017-01-04T17:26:21Z")

</div>

I found a solution which works really well:

```
void ControllerItem::ClearArc()
{
	//Checks if it is valid at index zero because index zero is always going to exist if the array has any elements
	if (splineMeshes.IsValidIndex(0))
	{
		while(splineMeshes.IsValidIndex(0))
		{
			//Debug message for checking to see if a spline was destroyed(which it is).
			UE_LOG(LogTemp, Warning, TEXT("DestroyComponent Called on object: %s"), *splineMeshes[0]->GetName());
			//This destroys the component but it does not remove it from the array.
			splineMeshes[0]->DestroyComponent();
			splineMeshes.RemoveAt(0);
		}
	}

	ArcSpline->ClearSplinePoints(true);
}
```

---

<div class="post-metadata">

**Author:** ![Load\_Time](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Load\_Time](https://forums.unrealengine.com/u/Load_Time)\
**Post date:** [February 18, 2017, 5:35am UTC](https://forums.unrealengine.com/t/destroying-an-array-of-splinemeshcomponents/380875/3 "2017-02-18T05:35:10Z")

</div>

You rock!!! Thank you for answering your post. This problem was testing my patience.
