UTexture2d deletion

I create my texture using the following line

LoadedT2D = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight());

And it loads correctly. LoadedT2D is stored in a UProperty(). So as long as the class which holds this UProperty() lives this texture should live. But in my case this texture is never getting destroyed even when the class which holds the UProperty is destroyed (destructor is called).

When I used Rama’s tutorial (A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums) to print out the references. It prints out AssetImportData and this is printed right after I create my texture with CreateTransient(). How do I avoid this reference to delete my texture.

Hello,

I was having the same issue. The solution that seems to work is running RemoveFromRoot and ConditionalBeginDestroy manually on the texture that you want to deallocate. The GarbageCollector will take care of it in this case.

UCLASS(Blueprintable)
class UMyClass: public UObject
{
	GENERATED_BODY()
...

	UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = Default)
	UTexture2D *SelectionTexture = nullptr;
}
...

void UMyClass::ClearSelectionTexture()
{
	if (nullptr != SelectionTexture)
	{
		SelectionTexture->RemoveFromRoot();
		SelectionTexture->ConditionalBeginDestroy();
	}

	SelectionTexture = nullptr;
}
1 Like