FCanvas, ERHIFeatureLevel and crashing

In constructor:

RenderTexture = Cast<UTextureRenderTarget2D>(StaticConstructObject(UTextureRenderTarget2D::StaticClass()));

RenderTexture->ClearColor = FLinearColor::Black;
RenderTexture->InitAutoFormat(128.0f, 128.0f);
RenderTexture->UpdateResourceImmediate();

TextureResource = (FTextureRenderTarget2DResource*)RenderTexture->Resource;

if (TextureResource == NULL)
	GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Green, FString::Printf(TEXT("No resource")));

Canvas = new FCanvas(TextureResource, NULL, GetWorld(), ERHIFeatureLevel::SM5);
Canvas->SetAllowedModes(FCanvas::ECanvasAllowModes::Allow_Flush);
Canvas->Clear(FLinearColor::Black);

In some method:

FCanvasBoxItem box(FVector2D(0.0f, 0.0f), FVector2D(100.0f, 100.0f));
box.SetColor(FLinearColor::Red);

FCanvasLineItem lined(FVector2D(0.0f, 0.0f), FVector2D(100.0f, 100.0f));
lined.LineThickness = 20.0f;

Canvas->DrawItem(box);
Canvas->DrawItem(lined);

Canvas->Flush_GameThread();

If I’m using anything but ERHIFeatureLevel::SM5, game crashing on Flush_GameThread().
UE 4.5 preview.

If this is the same issue I ran into the problem is that you hit an assertion IsGameThread triggered when the engine tries to rebuild shaders for a new platform.

My solution was to pass in the current render feature level:

FCanvas((FTextureRenderTarget2DResource*)RenderTexture->Resource, NULL, 0.0f, 0.0f, 0.0f, GetMaxRHIFeatureLevel())

To get this to link you may have to add RHI to your build dependencies - see myProject.Build.sc, extend PublicDependencyModuleNames

There is no such method GetMaxRHIFeatureLevel();

Now see GMaxRHIFeatureLevel

I’m trying to do something similar, and it works on the default cube, and I’m assuming on other flat meshes, but not on other surfaces. What can I do to fix that? Thanks.