Download

TArray::Num() Returning negative number and causing crash

Hi guys,

I’m trying to trace down a crash that keeps happening.

I think I’ve tracked it to a TArray that contains pointers.


TArray<ARunwayMaster*> CachedRunways;
TArray<AApronMaster*> CachedAprons;

If I debug to screen, Num() is 0 for a little while, or whatever I expect the number to be, but then the after a while it becomes a really big negative number.

It’s almost as if the game is discarding those TArrays. I’ve tried adding a plain UPROPERTY to both and that didn’t solve it.

Any ideas?

Okay so I think I have it sorted, but it’s a bit wierd.

It is asif the class itself was null

So in the game state class that manages it, I added UPROPERTY


UPROPERTY()
APlanePathingManager * PlanePathingManager;

Which seems to have solved it.

Thing is, I was already checking for null before hand:


if (PlanePathingManager)
{
    PlanePathingManager->UpdateRoutes();                
}


Yet, it was that UpdateRoutes function that was crashing

Here is the function that was crashing:



//Crash happens trying to empty
CachedRunways.Empty();
CachedAprons.Empty();

TArray<AActor*> FoundActors;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), APlaneNavigatableBuilding::StaticClass(), FoundActors);

    for (AActor * tmpActor : FoundActors)
    {
        ARunwayMaster * tmpRunwayMaster = Cast<ARunwayMaster>(tmpActor);

        if (tmpRunwayMaster)
        {
            CachedRunways.Add(tmpRunwayMaster);
        }

        AApronMaster * tmpApronMaster = Cast<AApronMaster>(tmpActor);

        if (tmpApronMaster)
        {
            CachedAprons.Add(tmpApronMaster);
        }
    }

GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, FString::Printf(TEXT("Num cached Runways: %i Num cached aprons: %i"), GetNumCachedRunways(), GetNumCachedAprons()));



When the breakpoint hits, go to the 'Auto’s window in Visual Studio.

If ‘this’ has some random value or is null, then the PlanePathingManager is being destroyed somehow.