FindByPredicate Crash

The following code works for a while, but then after a random period it will crash

	AActor** FoundEntry = PooledAgents.FindByPredicate([newClass](AActor*& InItem)
	{
//		UE_LOG(LogTemp, Error, TEXT("%s %s"), *InItem->GetClass()->GetName(), *newClass->GetName());
		if (InItem != nullptr)
			return false;

		return InItem->GetClass() == newClass;
	});

I debugged the data, and in

	template <typename Predicate>
	ElementType* FindByPredicate(Predicate Pred)
	{
		for (ElementType* RESTRICT Data = GetData(), *RESTRICT DataEnd = Data + ArrayNum; Data != DataEnd; ++Data)
		{
			if (Pred(*Data))
			{
				return Data;
			}
		}

		return nullptr;
	}

*Data is null in the if check. It could be that something is marked for delete or deleted, but shouldn’t there be a check for null on the *Data before trying to use it?

Hi,

The FindByPredicate code is fine. nullptr gets being passed to your predicate, which should also be fine, but I think the problem is your predicate: ‘if (InItem != nullptr)’ should use == instead, otherwise you will dereference a null pointer when you call GetClass().

Steve

Sorry I kept meaning to accept this. It’s absolutely the right answer. I had a derp moment and couldn’t spot that the if statement was wrong for the life of me. Thanks! Working for over an hour of gameplay time without crash now.