# If statement causes crashes when given anything declared in header

**URL:** <https://forums.unrealengine.com/t/if-statement-causes-crashes-when-given-anything-declared-in-header/468090>\
**Category:** Getting Started & Setup\
**Tags:** actor-component, unreal-engine, crash, question\
**Created:** [July 13, 2020, 9:10am UTC](https://forums.unrealengine.com/t/if-statement-causes-crashes-when-given-anything-declared-in-header/468090 "2020-07-13T09:10:24Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![BEnSon1101](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BEnSon1101](https://forums.unrealengine.com/u/BEnSon1101)\
**Post date:** [July 13, 2020, 9:10am UTC](https://forums.unrealengine.com/t/if-statement-causes-crashes-when-given-anything-declared-in-header/468090/1 "2020-07-13T09:10:24Z")

</div>

if statements **Inside Actor Component** cause crashes when given (int, bool, float) **that are declared in the actor components header**

**is this something to do with ActorComponent** and its header and the way things should be declared

charater .h

```
class UMyActorComponent* ActorComp;

```

character cpp

```
#include "MyActorComponent.h"

void AMyCharacter::BeginPlay()
{
	Super::BeginPlay();

	ActorComp->CompFunc();
}

```

ActorComponent .h

```
bool BB;

void CompFunc();

```

ActorComponent cpp

```
#include "Engine.h"

UMyActorComponent::UMyActorComponent()
{
	BB = true;
}

void UMyActorComponent::CompFunc()
{
	if (BB)
	{
		// Nothing
	}
	else
	{
		// nothing
	}
}
```

---

<div class="post-metadata">

**Author:** ![BEnSon1101](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BEnSon1101](https://forums.unrealengine.com/u/BEnSon1101)\
**Post date:** [July 13, 2020, 9:14am UTC](https://forums.unrealengine.com/t/if-statement-causes-crashes-when-given-anything-declared-in-header/468090/2 "2020-07-13T09:14:25Z")

</div>

the **crash Log** says Unhandled Exception: EXCEPTION\_ACCESS\_VIOLATION reading address 0x000000c0

---

<div class="post-metadata">

**Author:** ![EvilCleric](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@EvilCleric](https://forums.unrealengine.com/u/EvilCleric)\
**Post date:** [July 13, 2020, 9:26am UTC](https://forums.unrealengine.com/t/if-statement-causes-crashes-when-given-anything-declared-in-header/468090/3 "2020-07-13T09:26:10Z")

</div>

Is ActorComp valid?

```
if (IsValid(ActorComp))
{
   ActorComp->CompFunc();
}
```

---

<div class="post-metadata">

**Author:** ![BEnSon1101](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BEnSon1101](https://forums.unrealengine.com/u/BEnSon1101)\
**Post date:** [July 13, 2020, 9:32am UTC](https://forums.unrealengine.com/t/if-statement-causes-crashes-when-given-anything-declared-in-header/468090/4 "2020-07-13T09:32:31Z")

</div>

Wow i didn’t realize it wasn’t valid **Why is this** it is not valid

---

<div class="post-metadata">

**Author:** ![EvilCleric](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@EvilCleric](https://forums.unrealengine.com/u/EvilCleric)\
**Post date:** [July 13, 2020, 9:38am UTC](https://forums.unrealengine.com/t/if-statement-causes-crashes-when-given-anything-declared-in-header/468090/5 "2020-07-13T09:38:18Z")

</div>

How are you setting ActorComp? Is ActorComp a UPROPERTY?

Only use IsValid with UPROPERTY, otherwise, a simple nullptr check.

```
     if (ActorComp != nullptr)
     {
        ActorComp->CompFunc();
     }

```

Also, always initialize your pointers:

```
 class UMyActorComponent* ActorComp = nullptr;
```

---

<div class="post-metadata">

**Author:** ![BEnSon1101](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BEnSon1101](https://forums.unrealengine.com/u/BEnSon1101)\
**Post date:** [July 13, 2020, 9:51am UTC](https://forums.unrealengine.com/t/if-statement-causes-crashes-when-given-anything-declared-in-header/468090/6 "2020-07-13T09:51:40Z")

</div>

i have tried all of what you said but the ActorComponent isnt valid

character .h

```
class UMyActorComponent* ActorComp = nullptr;

```

Character cpp

```
if (ActorComp != nullptr)
{
	GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Purple, TEXT("actor comp valid"));
	//ActorComp->CompFunc();
}
```

---

<div class="post-metadata">

**Author:** ![EvilCleric](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@EvilCleric](https://forums.unrealengine.com/u/EvilCleric)\
**Post date:** [July 13, 2020, 10:08am UTC](https://forums.unrealengine.com/t/if-statement-causes-crashes-when-given-anything-declared-in-header/468090/7 "2020-07-13T10:08:18Z")

</div>

If it’s not valid, then how are you assigning ActorComp?

Are you creating a MyActorComponent component? How are you getting its reference?

---

<div class="post-metadata">

**Author:** ![BEnSon1101](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BEnSon1101](https://forums.unrealengine.com/u/BEnSon1101)\
**Post date:** [July 13, 2020, 10:12am UTC](https://forums.unrealengine.com/t/if-statement-causes-crashes-when-given-anything-declared-in-header/468090/8 "2020-07-13T10:12:36Z")

</div>

i thought

```
  class UMyActorComponent* ActorComp = nullptr;

```

\*\*was all i needed to do \*\*

and

```
  ActorComp->CompFunc();

```

was how i called functions in it actorcomponent

---

<div class="post-metadata">

**Author:** ![EvilCleric](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@EvilCleric](https://forums.unrealengine.com/u/EvilCleric)\
**Post date:** [July 13, 2020, 10:25am UTC](https://forums.unrealengine.com/t/if-statement-causes-crashes-when-given-anything-declared-in-header/468090/9 "2020-07-13T10:25:29Z")

</div>

I wish it was that simple.

Is MyActorComponent a component for MyCharacter? If so, then use the same process that is used to create any other component in the character’s construtor:

```
ActorComp = CreateDefaultSubobject<UMyActorComponent>(TEXT("MyActorComponent"));
ActorComp->SetupAttachment(RootComponent);

```

And:

```
 UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
 class UMyActorComponent* ActorComp = nullptr;

```

Which means, you now can use IsValid instead of nullptr to check if the pointer is valid.

---

<div class="post-metadata">

**Author:** ![BEnSon1101](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BEnSon1101](https://forums.unrealengine.com/u/BEnSon1101)\
**Post date:** [July 13, 2020, 10:50am UTC](https://forums.unrealengine.com/t/if-statement-causes-crashes-when-given-anything-declared-in-header/468090/10 "2020-07-13T10:50:31Z")

</div>

Thankyou \*\*But \*\* it is giving me an error with SetupAttachment

---

<div class="post-metadata">

**Author:** ![BEnSon1101](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BEnSon1101](https://forums.unrealengine.com/u/BEnSon1101)\
**Post date:** [July 13, 2020, 10:51am UTC](https://forums.unrealengine.com/t/if-statement-causes-crashes-when-given-anything-declared-in-header/468090/11 "2020-07-13T10:51:27Z")

</div>

it says UMyActorComponent has no member setup attachment

---

<div class="post-metadata">

**Author:** ![EvilCleric](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@EvilCleric](https://forums.unrealengine.com/u/EvilCleric)\
**Post date:** [July 13, 2020, 10:58am UTC](https://forums.unrealengine.com/t/if-statement-causes-crashes-when-given-anything-declared-in-header/468090/12 "2020-07-13T10:58:10Z")

</div>

Sorry my bad. Remove that line.

---

<div class="post-metadata">

**Author:** ![BEnSon1101](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BEnSon1101](https://forums.unrealengine.com/u/BEnSon1101)\
**Post date:** [July 13, 2020, 11:19am UTC](https://forums.unrealengine.com/t/if-statement-causes-crashes-when-given-anything-declared-in-header/468090/13 "2020-07-13T11:19:02Z")

</div>

Thank you so much **EvilCleric**

\*\*I was truely stumped \*\*

---

<div class="post-metadata">

**Author:** ![BEnSon1101](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BEnSon1101](https://forums.unrealengine.com/u/BEnSon1101)\
**Post date:** [July 13, 2020, 11:19am UTC](https://forums.unrealengine.com/t/if-statement-causes-crashes-when-given-anything-declared-in-header/468090/14 "2020-07-13T11:19:35Z")

</div>

That fixed it
