View Issue Details

IDProjectCategoryView StatusLast Update
0001220NoesisGUIUnitypublic2018-12-27 13:33
Reporterivan_b Assigned Tosfernandez  
PriorityhighSeveritymajor 
Status resolvedResolutionwon't fix 
Product Version2.1.0b15 
Target Version2.2.0 
Summary0001220: Memory issue
Description

In the Control gallery sample, when switching between two items, the memory usage grows and it is never released.
In general this occurs when instantiating new user controls, the memory is not released after the UserControl is not in use anymore.
I will try to reproduce this in an empty unity project.

Attached Files
PlatformAny

Activities

sfernandez

sfernandez

2018-01-18 00:34

manager   ~0005025

I'm trying to reproduce the problem with the ControlGallery and after switching between samples I see the memory growing, but after Garbage Collector kicks in memory is returned to the system.
Are you sure the memory keeps growing forever?

ivan_b

ivan_b

2018-01-20 15:56

reporter   ~0005033

I think I have located the problem, the problem is not when you load the xaml's as in the control gallery sample but when you use a UserControl and the inside a user control you use Noesis.GUI.LoadComponent.
I have uploaded a small sample.
When you switch multiple time between the two view you can see that the memory usage grows but when you click the button for garbage collection (it calls GC.Collect) the memory is not deallocated.

Noesis bug.rar (11,750 bytes)
sfernandez

sfernandez

2018-01-23 17:48

manager   ~0005043

Thanks for the project ivan... I'm investigating the issue.

sfernandez

sfernandez

2018-01-25 13:53

manager   ~0005051

Hi ivan,

The problem is related to the event hook (Initialized in your example).

Our current implementation requires to store the event handler in a static dictionary to communicate with native code. This is causing the delegate Target to remain strongly referenced and object is not released.
We know about this problem but the right solution is not totally clear as we don't want to harm event performance.

In your scenario this problem can be workaround by manually unregistering from the events when control is removed from the tree:

namespace NoesisBug
{
public class Menu : UserControl
{
public Menu()
{
this.Initialized += OnInitialized;
this.Unloaded += OnUnloaded;
this.InitializeComponent();
}

    private void InitializeComponent()
    {
        Noesis.GUI.LoadComponent(this, "Assets/MemoryBug/Menu.xaml");
    }

    private void OnInitialized(object sender, EventArgs args)
    {
    }

    private void OnUnloaded(object sender, RoutedEventArgs e)
    {
        Initialized -= OnInitialized;
        Unloaded -= OnUnloaded;
    }
}

}

ivan_b

ivan_b

2018-01-25 14:14

reporter   ~0005052

Thanks for the help.

I don't mind if it stays this way :).

sfernandez

sfernandez

2018-12-27 13:33

manager   ~0005378

I found another simple way to break the strong reference, to allow memory to be correctly disposed.
Instead of unregistering from the events, use an intermediate weak reference:

namespace NoesisBug
{
public class Menu : UserControl
{
public Menu()
{
WeakReference weak = new WeakReference(this);
this.MouseDown += (s, e) => { ((Menu)weak.Target)?.OnMouseDown(s, e); };

        this.InitializeComponent();
    }

    private void InitializeComponent()
    {
        Noesis.GUI.LoadComponent(this, "Assets/MemoryBug/Menu.xaml");
    }

    private void OnMouseDown(object sender, MouseButtonEventArgs args)
    {
    }
}

}


This will correctly destroy Menu instances, even when registering against own events.

Issue History

Date Modified Username Field Change
2018-01-17 18:52 ivan_b New Issue
2018-01-17 18:52 ivan_b Tag Attached: 2.1Beta
2018-01-17 20:41 sfernandez Assigned To => sfernandez
2018-01-17 20:41 sfernandez Status new => assigned
2018-01-18 00:34 sfernandez Status assigned => feedback
2018-01-18 00:34 sfernandez Note Added: 0005025
2018-01-20 15:56 ivan_b File Added: Noesis bug.rar
2018-01-20 15:56 ivan_b Note Added: 0005033
2018-01-20 15:56 ivan_b Status feedback => assigned
2018-01-23 17:48 sfernandez Note Added: 0005043
2018-01-25 13:53 sfernandez Status assigned => feedback
2018-01-25 13:53 sfernandez Note Added: 0005051
2018-01-25 14:14 ivan_b Note Added: 0005052
2018-01-25 14:14 ivan_b Status feedback => assigned
2018-11-01 02:14 jsantos View Status public => private
2018-11-22 11:58 sfernandez Target Version => 2.2.0
2018-11-22 11:58 sfernandez Platform => Any
2018-11-22 11:58 sfernandez View Status private => public
2018-12-27 13:33 sfernandez Status assigned => resolved
2018-12-27 13:33 sfernandez Resolution open => won't fix
2018-12-27 13:33 sfernandez Note Added: 0005378
2025-10-10 13:29 jsantos Category Unity3D => Unity