Weird ACCESS VIOLATION when using GEngine->AddOnScreenDebugMessage in C++ constructor

I’m writing a BFS algorithm which is a static method of a simple struct called Algorithm.

Here is the sample code:
void Algorithm::BFS(const T&, const FIntPoint&)
{
int a = 0;
GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Cyan, FString::FromInt(a));
GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Cyan, FString::FromInt(233))
}

That’s it, no issue found in compile, but after build the project and start editor, access violation happened, both debug function will generate same issue (by annotate one of two):

stack is as follows: <~Desensitized~>

UnrealEditor_Engine
UnrealEditor_<~PROJECTNAME~>!Algorithm::BFS()
<~some call path~>
UnrealEditor_CoreUObject

the second line shows the error line of the cpp files which indicate the issue is at AddOnScreenDebugMessage.

I encountered this issue in another situation, when debugging some variable in C++ default constructor.

Since UE Crash reporter doesn’t give more details of which line encounter this issue in Engine instance, thus I’m so confused and don’t know how to fix this.

Is there any idea why it happened or possible direction on debugging this issue?

Thanks.

Hi guys, I noticed that BFS is called from constructors of a AActor class, when I move it to Begin play, it works.

Looks like the constructor code runs before game engine instance is created.

I made another test, I put two DebugMessage call in same constructors but different location. And the result is the first one will encounter this violation while the last one won’t.

I guess this is a multi threading issue, when constructor running, there is no way to know if game engine instance is created or not.

So far so good, I can continue my code, but still wondering the true reason of this problem.