UE_Log crashing engine when trying to print FString

Hi all again,
I’m trying, in vain, to print a FString from a specific struct contained in a TArray, like this:

struct stuff { int32 first; FString second; };
TArray<stuff *> stuffList;

FString test = stuffList[x]->second; //x is a valid index
UE_LOG(LogTemp, Warning, TEXT("%s"), *test);

No matter what I try it crashes.
Funnily enough, even AnswerHub crashed when I posted this question here. Had to rewrite the post…

Couple of things. I’m assuming there is a part of the code between lines 2 and 4 where you actually initialize those pointers to stuff?

Also, check if it crashes when you do a UE_Log with just the text:

UR_Log(LogTemp, Warning, TEXT("Foobar"));

Finally, could you post the log?

Hey phoboz, no, it never crashes when I use it like that, or also when I try to print it like this: FString test = FString(TEXT(“test”)); and then UE_LOG %s *test .

I think the diagnostic could be one of these three (did a lot of tests in between and after so I’m not entirely sure). Sorry I can’t be more specific, didn’t really think about a possible bug. Hope this helps, in the meantime I used int32 instead of FStrings as workaround.

It looks like it’s the stuffList[x]->second that’s causing an issue. How are you initializing the array.

If possible, can you paste the whole code for that function?

Hey Klawd3-

You may be running into a scope issue depending on where the struct that is being added to the array is created in relation to where stuffList attempts to access the specific struct.

For example, using the following sets of code produced the following results:

Block1:

{
   Fstuff rando;
   rando.first = 777;
   rando.second = "Rando";
   stuffList.Add(&rando);

   Fstuff MyDefault;
   stuffList.Add(&MyDefault);
}

Block2:

{
   FString TestString;
   TestString= stuffList[0]->second;
   GEngine->AddOnScreenDebugMessage(-1, 4.f, FColor::Magenta, TestString);
   UE_LOG(LogTemp, Error, TEXT("%s"), *TestString);
}

When block1 is added to the constructor and block2 is placed inside of begin play, the editor will crash on play. If both blocks are placed in BeginPlay then the editor works as expected. In the first example, when focus leaves the constructor, the addresses where the rando and MyDefault structs were created are no longer in scope and are cleared out. When this address is then referenced in BeginPlay the value there does not match what is expected. If the structs are declared in the class declaration instead, the values of the struct variables can still be instantiated in the constructor but the struct will now be valid while inside the class regardless of which function is currently in scope. Thus, in the case of my sample code, moving the lines Fstuff rando; and Fstuff MyDefault' to the header prevented the crash from occurring.

Cheers

Doug Wilson