Game Engine Crash when checking TArray element

Hi,
I have been trying to get my TArray to work but it seems to end up crashing the game engine.

    // Header file 
    UPROPERTY(VisibleAnywhere)
	TArray<ATile*> GameTiles;

    // Source file
	GameTiles.Empty(GridWidth * GridHeight);
	GameTiles.AddUninitialized(GameTiles.Max());

    if(GameTiles[2]->IsValidLowLevel()) 
    {
       // Create Tile
       GameTiles[2] = CreateTile(TileLibrary[1].TileSprite, FVector(0.0f, -0.0f, -1140.0f), 0, 0)->SetActorScale3D(FVector(13.6,0,15.1));

    }

What i am trying to do here is to Test whether the GameTiles at a given index is empty. If its empty i will then Initialize a Tile and put it in GameTiles at the Tested index. But every time i try to check the GameTile the Game Engine will crash.

I am still very new to Unreal Engine 4. Any help would be greatly appreciated.

Thanks Iniside, the output log says:

Exception was "Access violation - code c0000005 (first/second chance not available)"

Source context from "engine/source/runtime/coreuobject/private/uobject/uobjectbase.cpp"

It seems that the Game accessing GameTiles at the index is accessing random garbage.
Is there a way to check if the index at GameTiles is Garbage hence return a bool value telling the Game that you can create a new object for GameTiles at that index. I have tried using try catch but the game is still crashing.

Any help is really appreciated.

Check last few lines in output log.
I guess, that object you are trying to check is some random garbage, since add Uninitialized does not really construct any objects, just fill array, to make indexes valid.

Build in debug and try to look what you really have in array.

Inside is right.

Your objects in your array are not constructed. Only the Array size was pre initialized.

Before you do the IsValidLowLevel(), you should test if the object exist via a nullptr check.

IsValidLowLevel expect a valid object. Cause it is a deeper check of integrity of an object.

Thanks .
I see GameTiles.AddUninitialized(GameTiles.Max()); is only pre initializing the array.

Here is how i am doing the test if the object exist:

if (GameTiles[SpawnLocation] == nullptr)
	{
		UE_LOG(LogClass, Log, TEXT("The Tile will spawn at %d"), currentLocation);
		return currentLocation;
	}

But the Game is crashing when it is doing the check. Is this the correct way to do checks on TArray. Any help is really greatly appreciated Thanks.

The output log:
Exception was “Unknown exception - code 00000001 (first/second chance not available)”

Source context from "mymobilematch3game/source/mymobilematch3game/grid.cpp"

Hey john45,

I was wrong with the nullptr check. Cause you use GameTiles.AddUninitialized(GameTiles.Max()); on pointer type childs. I am wondering, why you use AddUninitialized?

Would not GameTiles.AddZeroed(GameTiles.Max()) or GameTiles.AddDefaulted(GameTiles.Max()) more suitable for you? There you can do the nullptr check. Well and I would use IsValid(object*) for the nullptr check.

Hi ,

Thanks alot for your help!!!
Yup you are right i should use either GameTiles.AddZeroed(GameTiles.Max()) or GameTiles.AddDefaulted(GameTiles.Max())
To do the nullptr check. Right now the checks is not crashing the game engine ( :