4.12 C++ PrintString in HUD constructor causes editor crash on launch

Not sure if bug or I am doing something wrong. I have tracked this down to being the hud class. If I remove it from the source and rebuild the editor will launch. If I put the files in and rebuild the editor will crash on startup.


#pragma once

#include "GameFramework/HUD.h"
#include "RuckusHUD.generated.h"

class RUCKUSARENA_API ARuckusHUD : public AHUD
	ARuckusHUD(const class FObjectInitializer& ObjectInitializer);

	virtual void DrawHUD() override;

	void DrawCenterDot();

And RuckusHUD.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "RuckusArena.h"
#include "RuckusHUD.h"
#include "Runtime/Engine/Classes/Kismet/KismetSystemLibrary.h"

ARuckusHUD::ARuckusHUD(const class FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
	UKismetSystemLibrary::PrintString(this, "HUD init top", true, true, FLinearColor(0.000000, 0.660000, 1.000000, 1.000000), 2.000000);
	UKismetSystemLibrary::PrintString(this, "HUD init bottom", true, true, FLinearColor(0.000000, 0.660000, 1.000000, 1.000000), 2.000000);


void ARuckusHUD::DrawHUD()

void ARuckusHUD::DrawCenterDot()

And the editor crash log, just the last lines …

[2016.08.30-14.21.02:859][ 0]LogUObjectArray: CloseDisregardForGC: 0/0 objects in disregard for GC pool
[2016.08.30-14.21.03:175][ 0]LogSlate: SlateFontCache - WITH_FREETYPE: 1, WITH_HARFBUZZ: 1
[2016.08.30-14.21.03:175][ 0]LogSlate: SlateFontCache - WITH_FREETYPE: 1, WITH_HARFBUZZ: 1
[2016.08.30-14.21.04:163][ 0]LogUdpMessaging: Initializing bridge on interface to multicast group
[2016.08.30-14.21.04:227][ 0]LogBlueprintUserMessages: [Default__RuckusHUD] HUD init top

Note there is nothing after the HUD init top

If lines 12 and 14 are commented out in RuckusHUD.cpp the editor will launch fine. Also if you compile with the two prints with the editor open it will hot swap without an issue.

Hey BMoney-

The crash is due to the print string attempting to fire when the class default object (CDO) is generated when the project launches. If you are trying to print these messages when the game starts then you would want to either place the print string lines inside an if(GetWorld()) or place both prints in the BeingPlay function instead of DrawHUD.


Thanks for the workaround. Do we think that the editor should crash if the prints are in place without the world check?

Looking at the error message, PrintString is calling the engine’s AddOnScreenDebugMessage function. Because the engine is not fully established when this call is made, it is getting a value of NULL at the time of the call which is causing the error. In this case the crash is expected since it is seeing a function being called on something that technically doesn’t exist yet to prevent further errors from attempting to access something that hasn’t been created.