Changing Material from Code crashes when exiting (ESC/Stop)

I wanted to change the material on the blocks in the first person template.
In the constructor I have the following:

static ConstructorHelpers::FObjectFinder Material(TEXT(“MaterialInstanceConstant’/Game/FirstPerson/Materials/M_Tech_Hex_Tile_Pulse_Inst.M_Tech_Hex_Tile_Pulse_Inst’”));
if (Material.Object != NULL)
{
TheMaterial = (UMaterial*)Material.Object;
}

In the event method, i set the material the following way:

TheMaterial_Dyn = UMaterialInstanceDynamic::Create(TheMaterial, this);

Itr->SetMaterial(0, TheMaterial_Dyn);

When I press the ‘Q’ key (the one I bount to the event) the material is changed accordingly, no problem yet.
Exiting the game in the editor (ESC/Stop) gives me an access violation error.

Sometimes it breaks in the StaticMeshRenderer.cpp (line 2018):

OutMeshBatch.MaterialRenderProxy = Material->GetRenderProxy(bUseSelectedMaterial,bUseHoveredMaterial);

Sometimes it breaks in the ShaderBaseClasses.cpp (line 129), which is just “UE_LOG(”

If I destroy the Actors before exiting I don’t get the error.

What am I doing wrong? Do I need to edit the material at some point, do I need to import the material differently?

Seems like I found the answer:

When creating the Material, I used UMaterialInstanceDynamic::Create(TheMaterial, this)

Using this was wrong, should have used Itr->GetOuter() instead, since I was setting the UObject Outer wrong