David Jefferies
Topic Author
Posts: 27
Joined: 14 Jan 2021, 17:09

Unloading Textures

07 Sep 2023, 11:04

In Unreal we have a master view that contains an ItemsControl which holds all our views
  <UserControl.Resources>
        <ResourceDictionary>

            <DataTemplate x:Key="ViewTemplate">
	    	<ctrl:ViewLoader/>
	    </DataTemplate>

            <Style x:Key="ViewContainer-Style" TargetType="{x:Type ContentPresenter}">
				    <Setter Property="ContentTemplate" Value="{StaticResource ViewTemplate}"/>
            </Style>
        </ResourceDictionary>

	<ItemsControl x:Name="ViewsGenerator"
		  ItemsSource="{Binding ViewModelHolders}"
                  ItemContainerStyle="{StaticResource ViewContainer-Style}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>

In our custom viewloader control we load and unload the views like this:
void LoadView()
{
	Ptr<BaseComponent> component = GetXamlFile()->LoadXaml();
	FrameworkElement* element = DynamicCast<FrameworkElement*>(component.GetPtr());
	element->SetDataContext(GetViewModel());
	SetContent(component);
}

void UnloadView()
{
	SetContent(nullptr);
}

To unload the view we null the content which works visually but the xaml and the textures referenced in the xaml don't get unloaded - they are still reported as loaded by the GUI Inspector. What do we need to do to unload the textures?
 
User avatar
hcpizzi
Site Admin
Posts: 325
Joined: 09 Feb 2012, 12:40

Re: Unloading Textures

10 Sep 2023, 16:00

Hi!

I’m pretty sure the Inspector displays all the textures that have been loaded during the session, not just the currently loaded ones. It operates through the TextureProvider, and thus it only knows about textures when they are loaded, not when they are unloaded.

I’ll check the code and make sure that’s the way it works.
 
David Jefferies
Topic Author
Posts: 27
Joined: 14 Jan 2021, 17:09

Re: Unloading Textures

11 Sep 2023, 11:58

Thanks for the info, would be good to confirm that so we know not to use the Inspector for checking if a texture's unloaded

However In this case we know the textures aren't unloading because Unreal is reporting them still loaded even in a standalone build.

Can I confirm that what we're doing should cause the textures to be released?
And is there any way to debug why a texture is still loaded?
 
User avatar
hcpizzi
Site Admin
Posts: 325
Joined: 09 Feb 2012, 12:40

Re: Unloading Textures

11 Sep 2023, 17:47

Actually, looking at the code you shared, that cannot be the cause. The Noesis objects created won't keep the Unreal objects in memory.

Are you holding a reference to the NoesisXAML object? It contains hard references to the textures it uses, and it will prevent them from being unloaded.

Also, is the garbage collector running before you checked that the assets were still in memory?
 
David Jefferies
Topic Author
Posts: 27
Joined: 14 Jan 2021, 17:09

Re: Unloading Textures

19 Sep 2023, 19:04

Yes we were holding a reference to the UNoesisXaml object. Changing that reference to TSoftObjectPtr<UNoesisXaml> instead, and then doing Reset() to unload it fixed the issue.
Thanks :)

Who is online

Users browsing this forum: Ahrefs [Bot] and 8 guests