Repro for obscure canvas drawing crash UE-50111

I’m currently trying to find a proper fix for a crash I am running into. What I’ve done is the following:

  1. Create a new C++ AHUD class. The idea is to create a debug visualization HUD, based off of AAbilitySystemDebugHUD.
  2. The function that registers the drawing callback looks like:
    1. 
      	FDebugDrawDelegate DrawDebugDelegate = FDebugDrawDelegate::CreateUObject(HUD, &AMyDebugHUD::DrawDebugHUD);
      	// NOTE: GameplayDebug is not an arbitrary name. ShowFlags are defined in ShowFlagsValues.ini. We are reusing an
      	// existing one that's already used for things like the GAS debugger.
      	DrawDebugDelegateHandle = UDebugDrawService::Register(TEXT("GameplayDebug"), DrawDebugDelegate);
      
  3. Then, inside my DrawDebugHUD, I call another function I’ve made, DrawIcon:
    1. void AMyDebugHUD::DrawIcon(FString IconName, float& OffsetX, float& OffsetY)
      {
      	const FString Path = "/MyPluginConntentFolder/Resources/" + IconName + "." + IconName;
      	if (UTexture* T = LoadObject<UTexture2D>(nullptr, Path))
      	{
      		if (!ensure(T->GetResource())) return;
      		
      		FCanvasTileItem Icon(
      			FVector2D(OffsetX - 25, OffsetY - 30),
      			T->GetResource(),
      			FVector2D(25, 25),
      			FColor::White
      		);
      		Canvas->DrawItem(Icon);
      	}
      }
      
      1. I call it using DrawIcon("T_Icon_MapMarker", IconX, Y);, where T_Icon_MapMarker.uasset is a normal texture asset in my content folder. I’ve set its texture type to UI.
  4. Then when I enable my HUD via a console command in PIE, CRASH!

I’ve found a strange workaround for this, which is to

  1. launch Unreal with the DrawItem line commented out
  2. PIE, enable the HUD
  3. It works, as expected – no crash since we aren’t drawing the texture
  4. Exit PIE
  5. Uncomment the line, recompile using Live Coding
  6. PIE, enable the HUD
  7. Works flawlessly!

Pretty strange. Maybe there’s something missing in LoadObject<UTexture2D> where it doesn’t get fully initialized in the first usage.

I’m going to try some hacky fixes like delaying the drawing one frame and see if that helps.

According to https://issues.unrealengine.com/issue/UE-50111, this bug won’t be fixed since there isn’t a repro for it. Hopefully this helps!