Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tooltip's need revised for GC #941

Open
dlamkins opened this issue Feb 18, 2024 · 3 comments
Open

Tooltip's need revised for GC #941

dlamkins opened this issue Feb 18, 2024 · 3 comments
Labels
bug Something isn't working or doesn't work as expected.

Comments

@dlamkins
Copy link
Member

There is an issue with Tooltips that prevents them from being properly collected. Some initial investigation is discussed here:
https://discord.com/channels/531175899588984842/599270434642460753/1208556624902492250

@dlamkins dlamkins added the bug Something isn't working or doesn't work as expected. label Feb 18, 2024
@ynzenu
Copy link

ynzenu commented Feb 18, 2024

The problem is indeed the _allTooltips property on the Tooltip class. The solution could be as simple as removing the tooltip from the collection when it's disposed.

This is the solution I came up with for the Mystic Crafting module:

public class DisposableTooltip : Tooltip
   {
       public DisposableTooltip(ITooltipView tooltipView) : base(tooltipView) { }

       private void RemoveFromCollection()
       {
           var type = typeof(Tooltip);
           var field = type.GetField("_allTooltips", BindingFlags.NonPublic | BindingFlags.Static);

           if (field == null)
               return;

           var tooltips = (ControlCollection<Tooltip>)field.GetValue(null);

           if (tooltips == null)
               return;

           tooltips.Remove(this);
       }

       protected override void DisposeControl()
       {
           RemoveFromCollection();

           base.DisposeControl();
       }
   }

@ynzenu
Copy link

ynzenu commented Sep 28, 2024

@dlamkins I'm happy to help with this issue, but I'm not entirely sure how to solve the problem below.

Firstly, there are 3 issues I've found

  • Tooltip cannot be collected by GC because of static reference
  • Tooltip is not disposed
  • Menu is not disposed

Menu and Tooltip are not disposed, because they are not called in the Dispose method of Control.cs. They are also not children of a container (parent = null).

Is it safe to dispose the Tooltip and Menu when a control is disposed? The same Tooltip object could be assigned to multiple controls. For example:

var tooltip = new Tooltip()
var imageControl = new Image();
var labelControl = new Label();

imageControl.Tooltip = tooltip;
labelControl.Tooltip = tooltip;

imageControl.Dispose() //Tooltip should not be disposed because it's used multiple times?

The Tooltip.DisposeControl function also executes CurrentView?.DoUnload() while the view might be used elsewhere. I'm not sure what would be a good solution in this case.

Let me know what you think, but if the above is not an issue, then I can implement the following solution:

  • Remove tooltip static reference when disposed.
  • Dispose Tooltip and Menu when Control is disposed.

@ynzenu
Copy link

ynzenu commented Sep 28, 2024

Something else to note, is that tooltips are seemingly disposed when using the BasicTooltipView thanks an event initialized during build:

protected override void Build(Container buildPanel) {
        _tooltipLabel.Parent = buildPanel;

        buildPanel.Hidden += (sender, args) => buildPanel.Dispose();
}

I think we should change this as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working or doesn't work as expected.
Projects
None yet
Development

No branches or pull requests

2 participants