# TArray FindByPredicate causing access violation

**URL:** <https://forums.unrealengine.com/t/tarray-findbypredicate-causing-access-violation/434700>\
**Category:** C++\
**Tags:** tarray, access-violation, unreal-engine, CPP, question\
**Created:** [September 29, 2018, 6:55am UTC](https://forums.unrealengine.com/t/tarray-findbypredicate-causing-access-violation/434700 "2018-09-29T06:55:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DevilsD](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/devilsd/32/890854_2.png) [@DevilsD](https://forums.unrealengine.com/u/DevilsD)\
**Post date:** [September 29, 2018, 6:55am UTC](https://forums.unrealengine.com/t/tarray-findbypredicate-causing-access-violation/434700/1 "2018-09-29T06:55:59Z")

</div>

Hey guys.

Maybe im tired and cant see whats causing this but the following is what code ive got.

I cannot work out why the FindByPredicate is causing an access violation.

```
bool ASMFactionBase::DoesObjectClassMeetResourceRequirements(TSubclassOf<UObject> ObjectClass)
{
	if (!ObjectClass)
	{
		return false;
	}

	if (FactionTechTree)
	{
		//Find the GraphNode that the ObjectClass exists on so that we can query what its resource requirements are.
		UTechTreeGraphNode* ObjectNode = *(FactionTechTree->AllNodes.FindByPredicate([&](UTechTreeGraphNode* InNode)
		{
			return InNode->NodeAssetObject->IsChildOf(ObjectClass);
		}));

		if (ObjectNode && ResourceManagerComponent)
		{
			TArray<ASMResourceTypeBase*> Resources = ResourceManagerComponent->GetResources();

			for (const TPair<TSubclassOf<ASMResourceTypeBase>, int32>& ResourceRequirement : ObjectNode->ResourceRequirements)
			{
				//Find out if the Resource exists in the ResourceManager, if it doesnt then we cant continue because we obviously dont have the required resource.
				ASMResourceTypeBase* ResourceFromResourceManager = *(Resources.FindByPredicate([&](ASMResourceTypeBase* InResource) // <--------------- Access Violation on this Line.
				{
					return InResource->IsA(ResourceRequirement.Key);
				}));

				//If we do have the required resource, then find out if we have enough of it.
				if (ResourceFromResourceManager)
				{
					if (ResourceRequirement.Value > ResourceFromResourceManager->ResourceAmount)
					{
						return false;
					}
				}
				else
				{
					return false;
				}
			}
		}
		else
		{
			return false;
		}
	}

	return true;
}

```

If there is someone that can point out a possible issue with this please do reply 🙂

If you need more information let me know, i hope the comments are enough.

Thanks.

---

<div class="post-metadata">

**Author:** ![DevilsD](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/devilsd/32/890854_2.png) [@DevilsD](https://forums.unrealengine.com/u/DevilsD)\
**Post date:** [September 29, 2018, 7:21am UTC](https://forums.unrealengine.com/t/tarray-findbypredicate-causing-access-violation/434700/2 "2018-09-29T07:21:00Z")

</div>

Just to clarify, it seems to be caused when there is an Element in `ResourceRequirement` that is not in `Resources`

I dont understand why this would be causing it to occur. I was under the impression that the Predicate should just return an nullptr.

---

<div class="post-metadata">

**Author:** ![Firefly74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Firefly74](https://forums.unrealengine.com/u/Firefly74)\
**Post date:** [September 29, 2018, 11:02am UTC](https://forums.unrealengine.com/t/tarray-findbypredicate-causing-access-violation/434700/3 "2018-09-29T11:02:05Z")

</div>

Just to be sure, and exclude any potential crash from here:

instead of

```
{
             return InNode->NodeAssetObject->IsChildOf(ObjectClass);
}

```

make it

```
{
             if(!InNode || !InNode->NodeAssetObject) return false;
             return InNode->NodeAssetObject->IsChildOf(ObjectClass);
}

```

and tell us if it still crash

EDIT:  
sorry, took the wrong predicate…

```
{
       return InResource->IsA(ResourceRequirement.Key);
}

```

to

```
{
       if(!InResource)return false;
       return InResource->IsA(ResourceRequirement.Key);
}

```

also, when it crash, could you lood at the values inside “Resources” to check if there is a null / invalide /garbage collected one ?

---

<div class="post-metadata">

**Author:** ![DevilsD](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/devilsd/32/890854_2.png) [@DevilsD](https://forums.unrealengine.com/u/DevilsD)\
**Post date:** [September 29, 2018, 11:57pm UTC](https://forums.unrealengine.com/t/tarray-findbypredicate-causing-access-violation/434700/4 "2018-09-29T23:57:41Z")

</div>

I tried that earlier and it didnt help which is really weird, thats what makes no sense.

---

<div class="post-metadata">

**Author:** ![DevilsD](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/devilsd/32/890854_2.png) [@DevilsD](https://forums.unrealengine.com/u/DevilsD)\
**Post date:** [October 11, 2018, 3:44am UTC](https://forums.unrealengine.com/t/tarray-findbypredicate-causing-access-violation/434700/5 "2018-10-11T03:44:25Z")

</div>

Still having this issue. Anyone else can possibly explain it?

---

<div class="post-metadata">

**Author:** ![Firefly74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Firefly74](https://forums.unrealengine.com/u/Firefly74)\
**Post date:** [October 11, 2018, 10:00am UTC](https://forums.unrealengine.com/t/tarray-findbypredicate-causing-access-violation/434700/6 "2018-10-11T10:00:33Z")

</div>

Ok, found the answer, you are dereferencing a null pointer (thx captain obvious )

explanation :

you said : “it seems to be caused when there is an Element in ResourceRequirement that is not in Resources”  
and indeed, the find by predicate would return NULL in that case, but you still try to dereference the pointer ( of pointer ) in that case

Solution :

```
ASMResourceTypeBase* ResourceFromResourceManager =NULL;
auto result = Resources.FindByPredicate([&](ASMResourceTypeBase* InResource) 
                 {
                     return InResource->IsA(ResourceRequirement.Key);
                 });
if(result)
    ResourceFromResourceManager =*(result);
```

---

<div class="post-metadata">

**Author:** ![Dani216](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Dani216](https://forums.unrealengine.com/u/Dani216)\
**Post date:** [April 30, 2019, 10:08pm UTC](https://forums.unrealengine.com/t/tarray-findbypredicate-causing-access-violation/434700/7 "2019-04-30T22:08:22Z")

</div>

I had similar issue and this really helps me. 🙂
