Proper way to handle FSlateMaterialBrush


I’m trying to create a series of Slate brushes using parametric materials. My current code looks like this :

// Create material
UMaterial* Base = Cast<UMaterial>(FXXXStyleSet::GetIcon("MyMagnificentBrush")->GetResourceObject());
UMaterialInstanceDynamic* Instance = UMaterialInstanceDynamic::Create(Base , GetWorld());

// Create the brush dynamically
FSlateMaterialBrush Brush = FSlateMaterialBrush(*Instance, FVector2D(100, 100));

In this code, MyMagnificentBrush is an existing Slate brush with the appropriate material ; MaterialArray an array of material instances, BrushesArray an array of brushes. I merrily use those in the application and they work fine.

Only thing is, this code crashes on exiting ! FSlateRHIResourceManager::ReleaseDynamicResource() in slaterhiresourcemanager.cpp:650 asserts on IsA(), with obviously bad pointers as input.

I feel bad about the FSlateMaterialBrush(*Instance,…) part. I believe it to be wrong as I should be manipulating pointers, but the constructor seems to require a direct value.

Should I be creating and storing an instance of FSlateMaterialBrush, as opposed to a pointer or shared pointer ?

What is the proper way to do this ?

For those interested, just use a regular FSlateBrush and call SetResourceObject. That’s it.

Thanks a lot. I was struggling with this too.