Antrophy
Topic Author
Posts: 3
Joined: 02 Oct 2021, 16:21

Resource Dictionary memory leak

06 Mar 2022, 15:16

Hello, I've been investigating memory leaks because every time we closed the editor we got an ensure in Noesis::Shutdown method. The size of the memory leak was 32 bytes.

So what I did was that I deleted all the user controls, custom controls, custom converters etc. to just have a basic noesis plugin without any UI and start investigation from there. Yet, I was still getting the same memory leak. So I dug a bit deeper and found that in NoesisSettings.cpp there is a ResourceDictionary initialization in SetApplicationResources() method. I commented out the Resource dictionary initialization like this:
// ApplicationResourcesRef = LoadObject<UNoesisXaml>(nullptr, *ApplicationResources.GetAssetPathString(), nullptr, LOAD_NoWarn);
if (ApplicationResourcesRef)
{
	ApplicationResourcesRef->AddToRoot();
	uint32 Hash = ApplicationResourcesRef->GetContentHash();
	if (ApplicationResourcesHash != Hash)
	{
		// ApplicationResourcesHash = Hash;
		// Noesis::Ptr<Noesis::ResourceDictionary> Dictionary = Noesis::MakePtr<Noesis::ResourceDictionary>();
		// Noesis::GUI::SetApplicationResources(Dictionary);
		// ApplicationResourcesRef->LoadComponent(Dictionary);
	}
}
And I was no longer getting the memory leak. That's how I got to think that the issue is with the ApplicationResourcesRef static raw pointer. I tried to find where is that pointer getting deallocated but to no avail, so what I did was I created a destructor for NoesisSettings class and put this in it:
UNoesisSettings::~UNoesisSettings()
{
	Noesis::GUI::SetApplicationResources(nullptr);  
}
To my understanding, when SetApplicationResources gets a nullptr as a parameter, it calls Release() on the ResourceDictionary (as per the code). The memory leak hasn't appeared since so I guess that solved it.

So I wanted to ask what do you think of this solution? Do you think the problem is somewhere else but this deallocation just successfully hides it? If so, where do you think the issue could be?
Or do you think that this is a solution that will not potentially break anything in game and that you will include this fix in the later versions of Noesis?
I am asking because this is a change that is "deeper" in Noesis and I don't just want to change things in there in order to bandaid something else.

Thank you very much for your work and I'm looking forward to seeing your response.

Have a nice day


// EDIT:
We consulted within the team and since we have heavily modified unreal engine it may have got broken along the way somewhere else.

From my investigation, the Shutdown method in UI.cpp gets correctly called after closing down the editor. It does release the gApplicationResources as well but for some reason it either gets reallocated or the issue is somewhere else, the ensure still shows 32 bytes allocated. Tried putting a break point into SetApplicationResources to check if it gets called twice but it does not. My investigation came to an end. I will probably leave the destructor there as I don't think it's gonna break anything. If you guys have any idea where that issue could be, please give me a hint.
 
User avatar
hcpizzi
Site Admin
Posts: 321
Joined: 09 Feb 2012, 12:40

Re: Resource Dictionary memory leak

09 Mar 2022, 13:14

Hi,

Just to make sure I got this right: The leak is reported by Noesis when Noesis::GUI::Shutdown is called, right? So it's something allocated through our memory allocation callbacks that is leaking, not something on the Unreal side of things. Am I understanding it correctly?

Thanks
 
Antrophy
Topic Author
Posts: 3
Joined: 02 Oct 2021, 16:21

Re: Resource Dictionary memory leak

09 Mar 2022, 13:34

That is right, the leak is reported in the Init.cpp in the Noesis::Shutdown() method. By not having the destructor in NoesisSettings the noesis shutdown method displays 32 bytes allocated memory and by having the destructor with the Noesis::GUI::SetApplicationResources(nullptr); the memory leak goes away.
 
User avatar
jsantos
Site Admin
Posts: 3925
Joined: 20 Jan 2012, 17:18
Contact:

Re: Resource Dictionary memory leak

09 Mar 2022, 15:09

I see you have access to the source code, could we move this to a private ticket please? (also indicate, your company).

Who is online

Users browsing this forum: No registered users and 27 guests