Setting Texture Paramter Value leads to crash

Hey, so I am experimenting with setting a Texture Paramter inside a C++ class, but it crashes every time. My C++ source file inside the constructor

	static ConstructorHelpers::FObjectFinder<UStaticMesh> staticmesh_cube(TEXT("StaticMesh'/Engine/EditorMeshes/EditorCube.EditorCube'"));
	static ConstructorHelpers::FObjectFinder<UMaterial> qrmat(TEXT("Material'/Game/Voxel/Materials/M_QR.M_QR'"));
	static ConstructorHelpers::FObjectFinder<UTexture> texture(TEXT("Texture'/Engine/EngineMaterials/T_Default_Material_Grid_M.T_Default_Material_Grid_M'"));

	mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));

	UMaterial* material = qrmat.Object;
	UMaterialInstanceDynamic* minst = (UMaterialInstanceDynamic*)material;
	UTexture* Texture = texture.Object;
	minst->SetTextureParameterValue(FName(TEXT("QRTexture")), Texture);

	if (staticmesh_cube.Object)
		mesh->SetStaticMesh(staticmesh_cube.Object);

	if (qrmat.Object)
		mesh->SetMaterial(0, minst);

	mesh->SetWorldScale3D(FVector(0.1, 1, 1));
	mesh->bVisible = true;
	RootComponent = mesh;

The paramteter is just an TextureSampleParameter2D, and I get this crash message:

MachineId:AD538CEE405F8A26A01B3B910D50B2A2
EpicAccountId:d0fb234b758f4f36a97f7bdbbe031216

Access violation - code c0000005 (first/second chance not available)

""

UE4Editor_Engine!FMaterialRenderProxy::InvalidateUniformExpressionCache() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\runtime\engine\private\materials\materialshared.cpp:1652]
UE4Editor_Engine!FMaterialInstanceResource::RenderThread_UpdateParameter<UTexture const * __ptr64>() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\runtime\engine\private\materials\materialinstancesupport.h:118]
UE4Editor_Engine!EURCMacro_SetMIParameterValue<FTextureParameterValue>::DoTask() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\runtime\engine\private\materials\materialinstance.cpp:322]
UE4Editor_Engine!TGraphTask<EURCMacro_SetMIParameterValue<FTextureParameterValue> >::ExecuteTask() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\runtime\core\public\async\taskgraphinterfaces.h:779]
UE4Editor_Core!FTaskThread::ProcessTasks() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\runtime\core\private\async\taskgraph.cpp:539]
UE4Editor_Core!FTaskThread::ProcessTasksUntilQuit() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\runtime\core\private\async\taskgraph.cpp:340]
UE4Editor_RenderCore!RenderingThreadMain() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\runtime\rendercore\private\renderingthread.cpp:310]
UE4Editor_RenderCore!FRenderingThread::Run() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\runtime\rendercore\private\renderingthread.cpp:411]
UE4Editor_Core!FRunnableThreadWin::Run() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\runtime\core\private\windows\windowsrunnablethread.cpp:74]

If I uncomment the mesh->SetTextureParamterValue() line, it works…
What can I do?

Hello iUltimateLP,

I’m investigating this issue at the moment, but in the meantime, you may want to try moving mesh->SetTextureParameterValue() out of the constructor to either OnConstruct() or BeginPlay(). I remember seeing a recent change with Scalar Parameters being that way, but I’m unsure with Texture Parameters as of yet.

Hey thanks for the reply. I already fixed it by moving it into the OnConstruct(), thanks anyways!

7 years later. Same bug, same solution in UE5.