TArray insertion invalidates contents

I have a collection of pointers,

UPROPERTY(VisibleAnywhere)
TArray<UOObjects*> arr;

, and UOObject has a member array

UPROPERTY(VisibleAnywhere)
 TArray<AFOO*>

the following code invalidates elements in the foos array:

arr[foo->obj_index]->foos.Insert(foo, foo->foo_index);

before the code executes, indexes [0] and [1] has valid references to objects. When references are assigned (or inserted) to [2] and [3], [0] and [1] are set to NULL. Is this normal behavior for Insertion for TArray? or is something else going on?

it’s not normal that positions become null.
unless they get garbage collected.

arr[foo->obj_index]->foos.Insert(foo, foo->foo_index);

that line is pretty weird tbh.
maybe you should share a bit more of your code.

1 Like

@nande, thanks for the reply.
At a certain point in time t, a actor enters a collision box. OnOverlapBegin triggers then OnOverlapEnd triggers.

Here is some more code:

	UPROPERTY(VisibleAnywhere)
		TArray<UOObject*> arr;

	UPROPERTY()
		uint64 foo_section;
void AAObject::BeginPlay()
		{

                       uint8 section = calculated_section();
			if (section == 1) 
			{

				foo->obj_index = section;
				foo->previous_oobject_index = foo->obj_index;
				foo->foo_index = arr[section]->foos.Num();
				foo->previous_foo_index = foo->foo_index;
				arr[section]->foos.Insert(foo, foo->foo_index);
				arr[section]->number_of_foos++;
				arr[section]->index = section;
				
			}
			//...
			//...
			//...
			
		}
		
		void AAObject::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
		{
			
			Afoo* foo = Cast<Afoo>(OtherActor);
			UOoobject* oobject = Cast<UOoobject>(OverlappedComp);
			
			foo_section = oobject->index;
			foo->obj_index = oobject->index;
			foo->foo_index = arr[foo->obj_index]->foos.Num();
			
			if (arr[foo->obj_index]->foos.IsValidIndex(foo->foo_index))
			{
				if (arr[foo->obj_index]->foos[foo->foo_index] == nullptr)
					arr[foo->obj_index]->foos[foo->foo_index] = foo;
			
			
			}
			else
				arr[foo->obj_index]->foos.Insert(foo, foo->foo_index);


			arr[foo->obj_index]->number_of_foos++;
			
			
		}
		
	void AAObject::OnOverlapEnd(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) 
	{

	Afoo* foo = Cast<Afoo>(OtherActor);
	UOoobject* oobject = Cast<UOoobject>(OverlappedComp);

	if (OtherActor->IsA(Afoo::StaticClass())) 
	{

		foo->previous_oobject_index = oobject->index;
		if (arr[oobject->index]->foos.Num() == 0)
			return;
		else 
		{

			arr[oobject->index]->number_of_foos--;
			
			if (IsValid(arr[oobject->index]->foos[foo->previous_foo_index]))
			{

				arr[oobject->index]->foos[foo->previous_foo_index] = nullptr;
			
				foo->previous_oobject_index = foo->obj_index;
				foo->previous_foo_index = foo->foo_index;

			}
			else 
			{
				// This executes because [0] and [1] are now NULL

				GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Red, TEXT("[oobject ending] foo is not valid"));
				return;

			}

		}

	}

  }

this is a weird line to have.
to be honest i don’t understand what the code is supposed to do. the var names are very nondescriptive and confusing. can you share the actual code?

also can you share the actual definition of the whole class ? not just two floating variables. (you could strip other variables but it’s good to know other pieces too)

also please add the defintion of this “foos” variable