Materials created using UMaterialInstanceDynamic::Create cause crashes after a period of time.

Hey everyone,

Problem

This method causes the game to crash after what seems to be exactly 60 seconds. I have isolated it down to the UMaterialInstanceDynamic* objects, as the game does not crash if I use the base UMaterialInstance* objects.

Background

In one of my Slate widgets, I have a number of buttons that use dynamic materials. They all require different materials, as they all differ by a single parameter - the texture representing each button.

I have four material instances I have created in Blueprint, each which take in a texture parameter (“CustomTexture”) and alter it to create the Normal, Pressed, Hovered and Disabled ButtonStyles.

Each button uses the UMaterialInstanceDynamic::Create function to create a new dynamic material for each of the four material instances, passing their respective texture in as a material parameter before saving those dynamic materials into an FButtonStyle struct used in the construction of said button.

Things that might matter

The UMaterialInstanceDynamic* objects created by the Create function are declared inside of a for loop. I don’t think this is a problem as it LOOKS like they persist outside of said loop.

The UMaterialInstance* objects are created by the StaticLoadObject function, and are declared inside of the function. They work fine, which is why I didn’t think the above was a problem.

The FButtonStyle* objects are created on the heap, and the pointers are saved out in another class owned by the AHUD class.

This thread solved my issue:

So looks like TMap is no longer compatible with UPROPERTY. You must do a workaround by using TArray instead.