-
- KeldorKatarn
- Posts: 243
- Joined:
Accessing child in Decorator allocates memory every frame
My code:
Apparently the Child_get property getter is allocating memory every time it is called.
Code: Select all
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;
}
}-
- KeldorKatarn
- Posts: 243
- Joined:
Re: Accessing child in Decorator allocates memory every frame
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
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
-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: Accessing child in Decorator allocates memory every frame
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?
Could you store a reference to the Child in your DialogHostDecorator to see if that allocation disappears?
Code: Select all
Visual _child;
protected override Visual GetVisualChild(int index)
{
...
case 0:
_child = Child;
return _child;
...
}-
- KeldorKatarn
- Posts: 243
- Joined:
Re: Accessing child in Decorator allocates memory every frame
As I've pointed out, in the bug report, no that doesn't fix the issue
-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: Accessing child in Decorator allocates memory every frame
Solved #3970 for 3.2.8 version.
Who is online
Users browsing this forum: No registered users and 5 guests