Crash on editor start after adding C++ class

Starting from Blueprint only template, added custom AIController class to project via the editor. No other code in project.

First added this:
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT(“I Worked!”));

works fine. Crashes after compile here and crashes on each attempt to open project:
FVector myCharacter = GetControlledPawn()->GetActorLocation();
FVector testVector = FVector(100.0, 100.0, 100.0);
DrawDebugLine(GetWorld(), myCharacter, testVector, FColor::Yellow, false,100 , 0, 15.0);

Comment out all code and rebuild, crashes continue with the same error:

Dump Summary

Dump File: Dump-80018512.dmp : C:.…\Saved\Logs\Dump-80018512.dmp
Last Write Time: 5/20/2014 3:51:09 PM
Process Name: UE4Editor.exe : C:\Program Files\Unreal Engine\4.1\Engine\Binaries\Win64\UE4Editor.exe
Process Architecture: x64
Exception Code: 0xC0000005
Exception Information: The thread tried to read from or write to a virtual address for which it does not have the appropriate access.
Heap Information: Not Present

System Information

OS Version: 6.1.7601
CLR Version(s):

Replicated crash on a new project with same results.

Update:

I first figured that it was the DrawDebugLine that was crashing. But after starting a new project and only adding the on screen debug message, I’m getting the same crashes.

Here are the steps I’ve taken to get here:

  1. New Project → Code Top Down
  2. Create a new character blueprint using the “Hero” Mesh and animation
  3. Place character in the scene next to the player controlled default character
  4. Create a new class based on AIController → Save project close Editor
  5. Build project in VS2013 → Restart editor → Assign new AIController to character blueprint
  6. Added GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT(“I Worked!”)); to constructor in custom AIController
  7. Compile from Editor and test play. Get “I Worked” message. all is well
  8. Close Editor and open project up again and crash
  9. Comment out OnScreenDebugMessage in VS2013, build, open editor with no trouble

Seems like any code in the AIController causes the crash. Maybe calling GEngine is bad? Maybe my VS build is off? (MY UnrealVS plugin on the toolbar does not match the sample shown on Using the UnrealVS Extension for Unreal Engine C++ Projects | Unreal Engine 5.1 Documentation - So perhaps this is a sign of a build issue on VS2013’s end??)

Hi timconwell,

Any time you are accessing GEngine through code, it is a good idea to make sure it is available first. Try using the following code in your constructor:

if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("I Worked!"));
	}

Including this check will prevent the crash you are experiencing when you open the project.

Hey, you’re right. It did work. Ok, so will make sure that check is run for on screen messages. Still getting crash issues with DrawDebugLine. Tried putting that in the same GEngine check to see if it would make that work too.

Is there another game system that we should check for before calling DrawDebug functions?

Thanks!!!

Hi timconwell,

It looks like the crash is actually happening in the line FVector myCharacter = GetControlledPawn()->GetActorLocation();. I did some tinkering and managed to get the crash to stop happening. However, it does not look like it is doing what you want it to do. Namely, GetControlledPawn() does not seem to be actually returning a valid result. Here is the code I have to prevent the crash:

FVector MyCharacter = FVector(0.0, 0.0, 0.0);
	if (GetControlledPawn())
	{
		MyCharacter = GetControlledPawn()->GetActorLocation();
		
	}
	else
	{
		if (GEngine)
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("No controlled pawn"));
		}
	}

	FVector TestVector = FVector(200.0, 200.0, 200.0);
	DrawDebugLine(GetWorld(), MyCharacter, TestVector, FColor::Yellow, false, 100, 0, 15.0);

Notes:

  1. The if statement checking for a valid GetControlledPawn() result always seems to fail, so the default FVector is always used. You will need to do some additional troubleshooting on this.
  2. I increased the values of TestVector so the resulting debug line is actually visible (at the original 100.0 values it was drawn under the floor and could not be easily seen).

This code will prevent the crash when opening your project, but it will still need some work before it will do what it looks like you want it to do.

My respect and thanks for your help in this. I think I understand now, and in a way explains a curiosity I’ve had about several of my “Hello World” debug message tests in the past, appearing many times during startup. Appears as if on editor launch it’s running the game code. For whatever reason I figured it wouldn’t, but instead wait for “play” to be pressed once the editor is up.

Can’t help but to think this may be the cause of many of the crash on startup posts I’ve seen lately. But many, many thanks, you rock. Will in the future keep more environment checks going to not trip up editor loading. Thank you!!!