Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Editor/MenuActions/Geometry/BevelEdges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
PreviewActionManager.UpdatePreview();
}
});

PreviewActionManager.delayedPreviewChanged += () =>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Subscribing to a static event (PreviewActionManager.delayedPreviewChanged) using an anonymous delegate inside CreateSettingsContent will cause a memory leak. Because static events hold strong references to their subscribers, the floatField (and the entire parent VisualElement tree) will be kept alive in memory indefinitely even after the settings content is destroyed.

To prevent this, you should unsubscribe from the event when the visual element is detached. Have you considered registering a callback on the DetachFromPanelEvent to clean up the subscription?

Suggested change (apply manually):

            System.Action onDelayedPreviewChanged = () =>
            {
                floatField.isDelayed = PreviewActionManager.delayedPreview;
            };
            PreviewActionManager.delayedPreviewChanged += onDelayedPreviewChanged;
            root.RegisterCallback<DetachFromPanelEvent>(evt =>
            {
                PreviewActionManager.delayedPreviewChanged -= onDelayedPreviewChanged;
            });

🤖 Helpful? 👍/👎 by guardian

{
floatField.isDelayed = PreviewActionManager.delayedPreview;
};

Check warning on line 68 in Editor/MenuActions/Geometry/BevelEdges.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/MenuActions/Geometry/BevelEdges.cs#L64-L68

Added lines #L64 - L68 were not covered by tests
root.Add(floatField);

return root;
Expand Down
4 changes: 4 additions & 0 deletions Editor/MenuActions/Geometry/ExtrudeEdges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
floatField.tooltip = "Extrude Amount determines how far an edge will be moved along it's normal when extruding. This value can be negative.";
floatField.SetValueWithoutNotify(m_ExtrudeEdgeDistance);
floatField.RegisterCallback<ChangeEvent<float>>(OnExtrudeChanged);
PreviewActionManager.delayedPreviewChanged += () =>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Subscribing to a static event (PreviewActionManager.delayedPreviewChanged) using an anonymous delegate inside CreateSettingsContent causes a memory leak because the static event holds onto the visual elements even after they are discarded.

You can avoid this by unsubscribing from the event when the element is detached from the panel.

Suggested change (apply manually):

            System.Action onDelayedPreviewChanged = () =>
            {
                floatField.isDelayed = PreviewActionManager.delayedPreview;
            };
            PreviewActionManager.delayedPreviewChanged += onDelayedPreviewChanged;
            root.RegisterCallback<DetachFromPanelEvent>(evt =>
            {
                PreviewActionManager.delayedPreviewChanged -= onDelayedPreviewChanged;
            });

🤖 Helpful? 👍/👎 by guardian

{
floatField.isDelayed = PreviewActionManager.delayedPreview;
};

Check warning on line 59 in Editor/MenuActions/Geometry/ExtrudeEdges.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/MenuActions/Geometry/ExtrudeEdges.cs#L56-L59

Added lines #L56 - L59 were not covered by tests
root.Add(floatField);

return root;
Expand Down
5 changes: 5 additions & 0 deletions Editor/MenuActions/Geometry/ExtrudeFaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@
});
root.Add(distanceField);

PreviewActionManager.delayedPreviewChanged += () =>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Subscribing to a static event (PreviewActionManager.delayedPreviewChanged) using an anonymous delegate inside CreateSettingsContent causes a memory leak because the static event holds onto the visual elements even after they are discarded.

You can avoid this by unsubscribing from the event when the element is detached from the panel.

Suggested change (apply manually):

            System.Action onDelayedPreviewChanged = () =>
            {
                distanceField.isDelayed = PreviewActionManager.delayedPreview;
            };
            PreviewActionManager.delayedPreviewChanged += onDelayedPreviewChanged;
            root.RegisterCallback<DetachFromPanelEvent>(evt =>
            {
                PreviewActionManager.delayedPreviewChanged -= onDelayedPreviewChanged;
            });

🤖 Helpful? 👍/👎 by guardian

{
distanceField.isDelayed = PreviewActionManager.delayedPreview;
};

Check warning on line 113 in Editor/MenuActions/Geometry/ExtrudeFaces.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/MenuActions/Geometry/ExtrudeFaces.cs#L109-L113

Added lines #L109 - L113 were not covered by tests
return root;
}

Expand Down
8 changes: 6 additions & 2 deletions Editor/MenuActions/Geometry/OffsetElements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,19 @@

var distField = new Vector3Field("Translate");
distField.SetValueWithoutNotify(dist);
if(PreviewActionManager.delayedPreview)
distField.Query<FloatField>().ForEach(ff => ff.isDelayed = true);
distField.Query<FloatField>().ForEach(ff => ff.isDelayed = PreviewActionManager.delayedPreview);

Check warning on line 65 in Editor/MenuActions/Geometry/OffsetElements.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/MenuActions/Geometry/OffsetElements.cs#L65

Added line #L65 was not covered by tests
root.Add(distField);
distField.RegisterCallback<ChangeEvent<Vector3>>(evt =>
{
s_Translation.SetValue(evt.newValue, true);
PreviewActionManager.UpdatePreview();
});

PreviewActionManager.delayedPreviewChanged += () =>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Subscribing to a static event (PreviewActionManager.delayedPreviewChanged) using an anonymous delegate inside CreateSettingsContent causes a memory leak because the static event holds onto the visual elements even after they are discarded.

You can avoid this by unsubscribing from the event when the element is detached from the panel.

Suggested change (apply manually):

            System.Action onDelayedPreviewChanged = () =>
            {
                distField.Query<FloatField>().ForEach(ff => ff.isDelayed = PreviewActionManager.delayedPreview);
            };
            PreviewActionManager.delayedPreviewChanged += onDelayedPreviewChanged;
            root.RegisterCallback<DetachFromPanelEvent>(evt =>
            {
                PreviewActionManager.delayedPreviewChanged -= onDelayedPreviewChanged;
            });

🤖 Helpful? 👍/👎 by guardian

{
distField.Query<FloatField>().ForEach(ff => ff.isDelayed = PreviewActionManager.delayedPreview);
};

Check warning on line 76 in Editor/MenuActions/Geometry/OffsetElements.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/MenuActions/Geometry/OffsetElements.cs#L73-L76

Added lines #L73 - L76 were not covered by tests

return root;
}

Expand Down
4 changes: 4 additions & 0 deletions Editor/MenuActions/Geometry/SubdivideEdges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
m_SubdivCount.tooltip = tooltip;
m_SubdivCount.RegisterCallback<ChangeEvent<int>>(OnCountChanged);
m_SubdivCount.style.width = 40;
PreviewActionManager.delayedPreviewChanged += () =>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Subscribing to a static event (PreviewActionManager.delayedPreviewChanged) using an anonymous delegate inside CreateSettingsContent causes a memory leak because the static event holds onto the visual elements even after they are discarded.

You can avoid this by unsubscribing from the event when the element is detached from the panel.

Suggested change (apply manually):

            System.Action onDelayedPreviewChanged = () =>
            {
                m_SubdivCount.isDelayed = PreviewActionManager.delayedPreview;
            };
            PreviewActionManager.delayedPreviewChanged += onDelayedPreviewChanged;
            root.RegisterCallback<DetachFromPanelEvent>(evt =>
            {
                PreviewActionManager.delayedPreviewChanged -= onDelayedPreviewChanged;
            });

🤖 Helpful? 👍/👎 by guardian

{
m_SubdivCount.isDelayed = PreviewActionManager.delayedPreview;
};

Check warning on line 89 in Editor/MenuActions/Geometry/SubdivideEdges.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/MenuActions/Geometry/SubdivideEdges.cs#L86-L89

Added lines #L86 - L89 were not covered by tests
line.Add(foldout);
line.Add(m_Slider);
line.Add(m_SubdivCount);
Expand Down
4 changes: 4 additions & 0 deletions Editor/MenuActions/Geometry/WeldVertices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
m_WeldDistance.SetValue(evt.newValue);
PreviewActionManager.UpdatePreview();
});
PreviewActionManager.delayedPreviewChanged += () =>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Subscribing to a static event (PreviewActionManager.delayedPreviewChanged) using an anonymous delegate inside CreateSettingsContent causes a memory leak because the static event holds onto the visual elements even after they are discarded.

You can avoid this by unsubscribing from the event when the element is detached from the panel.

Suggested change (apply manually):

            System.Action onDelayedPreviewChanged = () =>
            {
                floatField.isDelayed = PreviewActionManager.delayedPreview;
            };
            PreviewActionManager.delayedPreviewChanged += onDelayedPreviewChanged;
            root.RegisterCallback<DetachFromPanelEvent>(evt =>
            {
                PreviewActionManager.delayedPreviewChanged -= onDelayedPreviewChanged;
            });

🤖 Helpful? 👍/👎 by guardian

{
floatField.isDelayed = PreviewActionManager.delayedPreview;
};

Check warning on line 75 in Editor/MenuActions/Geometry/WeldVertices.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/MenuActions/Geometry/WeldVertices.cs#L72-L75

Added lines #L72 - L75 were not covered by tests
root.Add(floatField);
return root;
}
Expand Down
5 changes: 4 additions & 1 deletion Editor/MenuActions/Selection/GrowSelection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@
m_GrowSelectionAngleIterative.SetValue(evt.newValue);
PreviewActionManager.UpdatePreview();
});

PreviewActionManager.delayedPreviewChanged += () =>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Subscribing to a static event (PreviewActionManager.delayedPreviewChanged) using an anonymous delegate inside CreateSettingsContent causes a memory leak because the static event holds onto the visual elements even after they are discarded.

You can avoid this by unsubscribing from the event when the element is detached from the panel.

Suggested change (apply manually):

            System.Action onDelayedPreviewChanged = () =>
            {
                floatField.isDelayed = PreviewActionManager.delayedPreview;
            };
            PreviewActionManager.delayedPreviewChanged += onDelayedPreviewChanged;
            root.RegisterCallback<DetachFromPanelEvent>(evt =>
            {
                PreviewActionManager.delayedPreviewChanged -= onDelayedPreviewChanged;
            });

🤖 Helpful? 👍/👎 by guardian

{
floatField.isDelayed = PreviewActionManager.delayedPreview;
};

Check warning on line 91 in Editor/MenuActions/Selection/GrowSelection.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/MenuActions/Selection/GrowSelection.cs#L88-L91

Added lines #L88 - L91 were not covered by tests
return root;
}

Expand Down
12 changes: 5 additions & 7 deletions Editor/Overlays/ActionSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
[UserSetting("Mesh Editing", "Auto Update Action Preview", "Automatically update the action preview, without delay. This operation is costly and can cause lag when working with large selections.")]
static Pref<bool> s_AutoUpdatePreview = new Pref<bool>("editor.autoUpdatePreview", false, SettingsScope.Project);
internal static bool delayedPreview => !s_AutoUpdatePreview.value;
public static event Action delayedPreviewChanged;

MenuAction m_CurrentAction;

Expand Down Expand Up @@ -59,8 +60,10 @@
ProBuilderEditor.selectModeChanged += SelectModeChanged;

SceneView.AddOverlayToActiveView(m_Overlay = new MenuActionSettingsOverlay());
// Hack to ensure that the overlay is displayed in the sceneview even when the overlay was already displayed before

Check warning on line 63 in Editor/Overlays/ActionSettings.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/Overlays/ActionSettings.cs#L63

Added line #L63 was not covered by tests
// Doing only displayed = true is not adding the overlay as it should in the view, only changing the display status is
m_Overlay.displayed = false;

Check warning on line 65 in Editor/Overlays/ActionSettings.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/Overlays/ActionSettings.cs#L65

Added line #L65 was not covered by tests
m_Overlay.displayed = true;
SceneView.RepaintAll();
}

public void Dispose()
Expand Down Expand Up @@ -104,12 +107,7 @@

s_AutoUpdatePreview.value = value;

if (s_Instance == null)
return;

SceneView.RemoveOverlayFromActiveView(s_Instance.m_Overlay);
SceneView.AddOverlayToActiveView(s_Instance.m_Overlay = new MenuActionSettingsOverlay());
s_Instance.m_Overlay.displayed = true;
delayedPreviewChanged?.Invoke();

Check warning on line 110 in Editor/Overlays/ActionSettings.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/Overlays/ActionSettings.cs#L110

Added line #L110 was not covered by tests
}

internal static void EndPreview()
Expand Down