Is variable initialization necessary?

If the question was obscure or silly, sorry about that. I’m almost out of mind spending all day long continuously encountering similar errors unsolved, and this is one of them.

Whenever I try to compile and play my project on the editor, Unreal Engine gets crashed and shows this.

Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x00000000000002d0
UnrealEditor_Engine
UnrealEditor_F15Test!APlayerC::AddActorToArray() [C:\Users\Park\Documents\Unreal Projects\F15Test 5.1\Source\F15Test\Private\PlayerC.cpp:205]
UnrealEditor_F15Test!APlayerC::CManageActorsInRange() [C:\Users\Park\Documents\Unreal Projects\F15Test 5.1\Source\F15Test\Private\PlayerC.cpp:122]
UnrealEditor_CoreUObject
UnrealEditor_CoreUObject
UnrealEditor_CoreUObject
UnrealEditor_CoreUObject
UnrealEditor_CoreUObject
UnrealEditor_CoreUObject
UnrealEditor_CoreUObject
UnrealEditor_CoreUObject
UnrealEditor_CoreUObject
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Core
UnrealEditor_Core
UnrealEditor_Core
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_UnrealEd
UnrealEditor_UnrealEd
UnrealEditor
UnrealEditor
UnrealEditor
UnrealEditor
UnrealEditor
UnrealEditor
kernel32
ntdll

As I searched on the internet, this EXCEPTION_ACCESS_VIOLATION error seems to occur in many other ways. This might include running the code without allocating memory to arrays or pointers, accessing wrong address due to variable initialization absence, and so on.
Since there are so many reasons, I first assumed this error comes from not giving default values(variable initialization absent). I didn’t do for most of the variables(or especially pointers and arrays).

This is part of my project’s header file.

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ManageActors")
	TArray<AActor*> GndAllyInRange;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ManageActors")
	TArray<AActor*> MissilesInRange;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ManageActors")
	TArray<AActor*> AirActorInCloseRange;

There are a bunch of properties,

UFUNCTION(BlueprintCallable)
	void CManageActorsInRange();
	
	UFUNCTION(BlueprintCallable)
		void CUpdatePosition();

	UFUNCTION(BlueprintCallable)
		void AddActorToArray(TArray<AActor*> ActorInRange, AActor* OtherActor);

and some functions.

If necessary, what should I put in for each types’ initialization?(ex: 0.0f for floats)

also if you need some other codes or additional explanations, I’ll upload it ASAP.

Hi hib0823,

I’d recommend you build your project in “Debug Mode” (from visual studio) and Run-Debug rather than Execute.

That way Visual Studio will break on that line (205) and you will have access to all the variables and the call stack - this will help a great deal in locating the problem.

Without seeing the code for AddActorToArray it’s hard to tell what would be happening.
using the “check()” macro for OtherActor and testing that the ActorInRange TArray is populated would be the first things though…

If it’s reference to the code that you post in other thread, then:
Line 205:

	if(!UKismetSystemLibrary::IsValid(CurrentTarget)&&((CurrentTarget->ActorHasTag("AIR")&&AirOrGround==0)||(CurrentTarget->ActorHasTag("GND")&&AirOrGround==1)))

Who and when sets CurrentTarget variable?

BTW This function doesn’t do what you expect.

You pass parameters by value, so. the function operates on copies and any operation has no effect to array passed in.

CurrentTarget is set by other blueprints such as the FindTarget, which is being implemented by blueprints.
I didn’t have confidence on passing pointer parameters so it was hard to determine am I doing right. How should I fix that issue using copies instead of the real address of actors(arrays)?

Hello!

  1. If the code Emaer posted is correct there is a mistake in that if statement on 205 line, because now basically what you’re doing is checking if the CurrentTarget is NOT valid and then if that is true(if it’s not valid) then you check variables in it, so you’re trying to access nullptr and that is why you get EXCEPTION_ACCESS_VIOLATION error.

To sum up to fix it you need to delete “!” from the beginning of check.

if(UKismetSystemLibrary::IsValid(CurrentTarget) && 
((CurrentTarget->ActorHasTag("AIR") && AirOrGround==0) || 
(CurrentTarget->ActorHasTag("GND") && AirOrGround==1)))

Why is that enough? Well that’s a great question. If the “if” statements consist of more than one condition and they are seperated by AND(&&) the program will preform checks in a row and if one condition fails the whole check will be abandonded so your code after false validation won’t execute. That way we are safe from any more nulls :smiley: .

  1. Emaer is right at the moment your Array in that method is a copy of the one you’re passing, to fix that you need to pass a reference to the array.

That is how your method declaration should look like.

void AddActorToArray(TArray<AActor*>& ActorsInRange, AActor* OtherActor)
1 Like