Download

Attempting to log debug messages causing crash

I’m probably missing something simple here but when attempting to log messages using UE_LOG I’m getting a crash the second time it attempts to log. I can see that it logged one message and then when the next loop runs it immediately crashes. Using Visual Studio and debugging I can see that CurVect is getting the proper values but it is saying that (after the second frame runs as mentions above) it is crashing on setting a value to CurVect.

If there is no log message it doesn’t crash.



TArray<FVector2D>* TempArray;
FVector2D CurVect;

TempArray = AMPC->ReturnPressedLocations();
if (TempArray != NULL)
{
for (int32 b = 0; b < TempArray->Num(); b++)
{
	if (TempArray**.GetData() != NULL)
	{
		CurVect = *TempArray**.GetData();
		//UE_LOG(LogLevel, Warning, TEXT("Log message that doesn't crash"));
		UE_LOG(LogClass, Log, TEXT("Log message that crashes: %f"), CurVect.X);
		//UE_LOG(LogLevel, Warning, TEXT("Log message that also crashes: %s"),*CurVect.ToString())
	}
}
}


Any help is appreciated. I’m hoping it’s just something simple I’m missing.

probably


CurVect = *TempArray**.GetData();

this line of code.Why casting it like that?

Without it I get: (error C2679: binary ‘=’ : no operator found which takes a right-hand operand of type ‘FVector2D *’ (or there is no acceptable conversion))

I could be wrong but is that not the proper way to dereference a pointer?

For reference this is what I have and what I’m trying to do:



*TArray<FVector2D> PressedLocations;* - under protected in one of my classes. A dynamic array of 2d Vectors.

*TArray<FVector2D>* ReturnPressedLocations();* - Function I am using to return the values in the array. I believe this should be returning a reference to the dynamic array.

*TArray<FVector2D>* AMyProject2PlayerController::ReturnPressedLocations()
{
	return &PressedLocations;
}*




--Separate Class--
TArray<FVector2D>* TempArray;
AMyProject2PlayerController* AMPC = Cast<AMyProject2PlayerController>(GetOwningPlayerController());

if (AMPC != NULL)
{
	TempArray = AMPC->ReturnPressedLocations();
	if (TempArray != NULL)
	{
		FVector2D CurVect;
		for (int32 b = 0; b < TempArray->Num(); b++)
		{
			if (TempArray**.GetData() != NULL)
			{
				CurVect = *TempArray**.GetData();
				//Crash spot if there are log messages here trying to access info about CurVect
			}
		}

		UE_LOG(LogLevel, Warning, TEXT("Temparray isn't none. Length is: %i"), TempArray->Num());
	}
}


In hindsight it looks like I’ve done a few things wrong and that I should continue to read up about pointers, references, and the such. If anyone could still point me in the right direction of what I specifically did wrong though, I’d appreciate it.

Actually you dont need to deference-reference those like that.Have a look at this.



TArray<FHitResult> fHitRes;
......
bool result = w->SweepMulti(fHitRes,rayStart,rayStart+radius,FQuat::Identity, ECollisionChannel::ECC_PhysicsBody, shape, fqp);
if(result)
{
     for( int i = 0; i < fHitRes.Num(); ++i )
     {
			AActor* hitActor = fHitRes*.GetActor();
			if( hitActor )
                        {
                         ..............
                        }
     }


Why are you declaring your array as a pointer? AFAIK arrays in C++ are already pointers.



return &PressedLocations;


What you do here,you return the PressedLocations’s memory address but you dont use the memory address in order to use the information later.

It seems the only difference there is that the AActor hitActor* is declared as a pointer while my FVector2D isn’t. A quick fix changing it to FVector2D CurVector* and removing the dereference allows it to work fine. Are these not technically the same thing though as declaring it not as a pointer and dereferencing TempArray to it?

In regards to the TempArray being declared as a pointer I thought that it was the more ‘optimized’ way to do it - point to the reference of all the variables and see what they were rather than directly copying them all over. Am I doing this part incorrectly?

I believe its because dynamic arrays(in case TArray) are already pointers in the background.So you dont initialize them as a pointer again.