KeldorKatarn
Topic Author
Posts: 243
Joined: 30 May 2014, 10:26

Accessing child in Decorator allocates memory every frame

17 Feb 2025, 23:32

My code:
    public class DialogHostDecorator : Decorator
    {
        private readonly DialogHost _dialogHost = new();

        public DialogHostDecorator()
        {
            AddVisualChild(_dialogHost);
        }

        /// <summary>Gets the <see cref="Visual"/> children count.</summary>
        protected override int VisualChildrenCount
        {
            get
            {
                return Child is null ? 0 : 2;
            }
        }

        /// <inheritdoc/>
        protected override Size ArrangeOverride(Size finalSize)
        {
            base.ArrangeOverride(finalSize);
            var finalSizeRect = new Rect(finalSize);

            _dialogHost.Arrange(finalSizeRect);

            return finalSize;
        }

        /// <inheritdoc/>
        protected override Visual GetVisualChild(int index)
        {
            if (Child == null)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }

            switch (index)
            {
                case 0:
                    return Child;

                case 1:
                    return _dialogHost;

                default:
                    throw new ArgumentOutOfRangeException(nameof(index));
            }
        }

        /// <inheritdoc/>
        protected override Size MeasureOverride(Size constraint)
        {
            var desiredSize = base.MeasureOverride(constraint);
            _dialogHost.Measure(constraint);

            desiredSize = new Size(
                Math.Max(desiredSize.Width, _dialogHost.DesiredSize.Width),
                Math.Max(desiredSize.Height, _dialogHost.DesiredSize.Height));

            return desiredSize;
        }
    }
Apparently the Child_get property getter is allocating memory every time it is called.
 
KeldorKatarn
Topic Author
Posts: 243
Joined: 30 May 2014, 10:26

Re: Accessing child in Decorator allocates memory every frame

17 Feb 2025, 23:36

I reported a bug on this: https://www.noesisengine.com/bugs/view.php?id=3970

If there's a way to avoid the allocation, let me know. (It's 1.5 kB every frame, so everything but trivial)
You can't see it in the callstack below of the Unity Profiler, but if you expand that further, it all happens in that one decorator
Memory.jpg
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: Accessing child in Decorator allocates memory every frame

19 Feb 2025, 11:35

My guess is that the Child is not referenced in the managed world, only in the native instance of the Decorator. So everytime it is requested in the GetVisualChild, a new proxy is created for that child.

Could you store a reference to the Child in your DialogHostDecorator to see if that allocation disappears?
Visual _child;

protected override Visual GetVisualChild(int index)
{
  ...
  case 0:
    _child = Child;
    return _child;
  ...
}
 
KeldorKatarn
Topic Author
Posts: 243
Joined: 30 May 2014, 10:26

Re: Accessing child in Decorator allocates memory every frame

01 Mar 2025, 21:19

As I've pointed out, in the bug report, no that doesn't fix the issue
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: Accessing child in Decorator allocates memory every frame

23 May 2025, 10:30

Solved #3970 for 3.2.8 version.

Who is online

Users browsing this forum: No registered users and 5 guests